Jump to content




java.lang.ClassCastException


  • You cannot reply to this topic
9 replies to this topic

#1 djokoss22

  • New Members
  • 2 posts

Posted 06 January 2016 - 11:27 AM

hi, i am currently writing a code that send informations received from several advanced computers to a screen from Mekanism

i have an error : "Java exception trown: java.lang.ClassCastException"

pannel = peripheral.wrap("bottom")
rednet.close("back")
rednet.open("back")
function receiver()
channel,msg,protocol = rednet.receive()
if channel==35 then
  time = msg
elseif channel==54 and msg=="BGon" then
  state = msg
elseif channel==54 and msg=="BGoff" then
  state = msg
elseif channel==54 and protocol=="RF" then
  energy = msg
end
end
function check()
  if state=="BGon" then
  pannel.setText("clock",time,0x00FF00)
  pannel.setText("BGstate","BigReactor is powered on",0x00FF00)
  pannel.setText("BGenergy",energy,0x00FF00)
  elseif state=="BGoff" then
  pannel.setText("clock",time,0x00FF00)
  pannel.setText("BGstate","BigReactor is powered off",0xFF0000)
  pannel.setText("BGenergy",energy,0x00FF00)
end
end
while true do
receiver()
check()
term.setCursorPos(1,1)
term.clearLine()
term.write("clock:"..time.." energy:"..energy.." state:"..state)
end

this error come from this part of code:
function check()
  if state=="BGon" then
  pannel.setText("clock",time,0x00FF00)
  pannel.setText("BGstate","BigReactor is powered on",0x00FF00)
  pannel.setText("BGenergy",energy,0x00FF00)
  elseif state=="BGoff" then
  pannel.setText("clock",time,0x00FF00)
  pannel.setText("BGstate","BigReactor is powered off",0xFF0000)
  pannel.setText("BGenergy",energy,0x00FF00)
end

but if i change the "elseif" part of this fonction and use text instead of variables, i do not have errors anymore
ex:
function check()
  if state=="BGon" then
  pannel.setText("clock",time,0x00FF00)
  pannel.setText("BGstate","BigReactor is powered on",0x00FF00)
  pannel.setText("BGenergy",energy,0x00FF00)
  elseif state=="BGoff" then
  pannel.setText("clock",[color=#ff0000]"text here"[/color],0x00FF00)
  pannel.setText("BGstate","BigReactor is powered off",0xFF0000)
  pannel.setText("BGenergy",[color=#ff0000]"text here"[/color],0x00FF00)
end

i know my script could be better writed but i am a newbie.
i just want to know why this error appear and what cause it to appear.


thank's a lot
(sorry for my poor english)

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 06 January 2016 - 02:15 PM

Before your script sets time/energy, those variables are nil. pannel.setText() probably can't handle nil values.

Maybe just add a line to the top of your script to give them "default" values, like this:

local time, energy = "unknown", "unknown"


#3 djokoss22

  • New Members
  • 2 posts

Posted 06 January 2016 - 05:38 PM

I have already try to set variables and nothing changed. I tried what you told me but it does not work either.

i made some extra tests and the problem appear when the computer receive the state "BGon" or "BGoff" and try to set the energy text on the mekanism screen.

so, i tried various things and i solved the problem by sending energy value in string as follow:
function check()
  if state=="BGon" then
  pannel.setText("clock",time,0x00FF00)
  pannel.setText("BGstate","BigReactor is powered on",0x00FF00)
  pannel.setText("BGenergy",tostring(energy),0x00FF00)
  elseif state=="BGoff" then
  pannel.setText("clock",time,0x00FF00)
  pannel.setText("BGstate","BigReactor is powered off",0xFF0000)
  pannel.setText("BGenergy",tostring(energy),0x00FF00)
end

thank's for your help btw.

#4 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 06 January 2016 - 05:51 PM

What means 0x00FF00?
Please give the exact line number.

#5 hbomb79

  • Members
  • 352 posts
  • LocationOrewa, New Zealand

Posted 06 January 2016 - 08:25 PM

View PostSewbacca, on 06 January 2016 - 05:51 PM, said:

Please give the exact line number.

I believe that not all errors give line numbers. I have never had a Java exception that had a line number.

#6 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 06 January 2016 - 08:37 PM

0x00ff00 is a hex color code.

#7 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 06 January 2016 - 09:17 PM

After else or any statements end at the end is needed.

Example:
if state == true then
   ...
elseif state == 123 then
   ...
end
You did:
if state == true then
   ...
elseif state == 123 then
   ...

Edited by LeDark Lua, 06 January 2016 - 09:18 PM.


#8 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 06 January 2016 - 09:42 PM

View PostLeDark Lua, on 06 January 2016 - 09:17 PM, said:

-snip-

It's correct in his full code though. Indentation is just wrong.

#9 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 06 January 2016 - 09:53 PM

Maybe, you get a wrong value:

function receiver()
channel,[i][color=#b22222]msg[/color][/i],protocol = [color=#0000ff]rednet.receive()[/color]
if channel==35 then
  [b][color=#ff0000]time [/color][/b]= [i][color=#b22222]msg[/color][/i]
elseif channel==54 and msg=="BGon" then
  state = msg
elseif channel==54 and msg=="BGoff" then
  state = msg
elseif channel==54 and protocol=="RF" then
  [b][color=#ff0000]energy [/color][/b]= [i][color=#b22222]msg[/color][/i]
end
end

  pannel.setText("clock",[b][color=#ff0000]time[/color][/b],0x00FF00)
  pannel.setText("BGstate","BigReactor is powered off",0xFF0000)
  pannel.setText("BGenergy",[b][color=#ff0000]energy[/color][/b],0x00FF00)

Check your send message

#10 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 06 January 2016 - 10:46 PM

View Postdjokoss22, on 06 January 2016 - 05:38 PM, said:

i solved the problem






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users