This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
djokoss22's profile picture

java.lang.ClassCastException

Started by djokoss22, 06 January 2016 - 10:27 AM
djokoss22 #1
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)
Bomb Bloke #2
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"
djokoss22 #3
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.
Sewbacca #4
Posted 06 January 2016 - 05:51 PM
What means 0x00FF00?
Please give the exact line number.
hbomb79 #5
Posted 06 January 2016 - 08:25 PM
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.
KingofGamesYami #6
Posted 06 January 2016 - 08:37 PM
0x00ff00 is a hex color code.
LeDark Lua #7
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 on 06 January 2016 - 08:18 PM
KingofGamesYami #8
Posted 06 January 2016 - 09:42 PM
-snip-

It's correct in his full code though. Indentation is just wrong.
Sewbacca #9
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
Bomb Bloke #10
Posted 06 January 2016 - 10:46 PM
i solved the problem