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

Java Exeption Thow

Started by asnarr204, 04 February 2015 - 10:17 PM
asnarr204 #1
Posted 04 February 2015 - 11:17 PM
I have only just started playing computercraft, so I only know how to do so much. I am currently trying to write a program that uses the open peripherals terminal glasses. So far, it displayes my base's power on the glasses, and accepts commands: for example "$$mine 3" in chat.However, I have run into trouble with the command part. The program first stores the command in the "command" string, then breaks it up into the seperate strings "commandString" and "commandNum" (I had to look up how to do that part), so if someone was to type "$$mine 4" into chat, the "command" string would be "mine 4", the "commandString" string would be "mine", and the "commandNum" string would be "4". The program then tests for if "commandString" equals "mine" and if it does commandNum is converted to a number. Here is where the problem starts:

I want commandNum to equal the amount of iterations in an automated mining process. However, if I set iterations equal to commandNum, then try to compare iterations to a number this error comes up: "comCenter:139: attempt to compare string with number expected, got string". I tried to use an in-between variable called tempNum ((tempNum = commandNum +1) then (iterations = tempNum - 1)), but that just resulted in this error: "Java exeption thrown".

Anyone have insight to the problem? Here is the pastebin: http://pastebin.com/3pKBBg3p
Bomb Bloke #2
Posted 05 February 2015 - 03:03 PM
Line 41; did you mean?:

commandNum = tonumber(commandNum)
asnarr204 #3
Posted 05 February 2015 - 10:29 PM
Line 41; did you mean?:

commandNum = tonumber(commandNum)

Just tried your suggestion, still getting the Java thrown error.

Sorry about the late response, I only just got back home from school.
asnarr204 #4
Posted 05 February 2015 - 10:32 PM
also sorry about that last post being even later than anticipated, it had to get approved by a moderator, being my second post on this forum. This one should be the last one that has to be approved by a moderator
valithor #5
Posted 05 February 2015 - 11:42 PM
Line 41; did you mean?:

commandNum = tonumber(commandNum)

Just tried your suggestion, still getting the Java thrown error.

Sorry about the late response, I only just got back home from school.

You need the same thing on line 38.
commandNum = tonumber(i)


Also on line 41 you convert commandNum to a number, but never do anything with it. You would actually want to do something like this.
commandNum = tonumber(commandNum)

One thing to note about tonumber. If what you pass to it can be converted to a number, then it will convert it otherwise it will return nil.
So say you pass "5" to tonumber it will return 5, but if you pass something like "hello" to tonumber it will return nil.

– Over explanation below

Your problem is as the error says trying to compare a string and a number. With that in mind 5 does not equal "5", which is why you would want to tonumber("5") to receive 5. With this in mind we can compare it to your script.

In your function checkCommand() from lines 30-49 you use for i in pairs string.gmatch(command, "%S+") do this will loop through each thing that matches the patern, but i will ALWAYS be a string. So when you set commandNum to i on line 38 you are actually setting it to a string instead of a number.
Edited on 05 February 2015 - 10:51 PM
Bomb Bloke #6
Posted 05 February 2015 - 11:49 PM
Well, not on line 38. Apparently he only wants the conversion done if commandString is of a certain value, for whatever reason.

I suggest going back to a version of the script from before the "tempNum" stuff went in, then implementing the tonumber fix there.
asnarr204 #7
Posted 06 February 2015 - 12:00 AM
Well, not on line 38. Apparently he only wants the conversion done if commandString is of a certain value, for whatever reason.

I suggest going back to a version of the script from before the "tempNum" stuff went in, then implementing the tonumber fix there.
I wanted to make sure it would not run an error message and stop the program if the command was mis-typed and then it tried to convert the second part to a number, but that is not much of an issue.

Valithor, your comment was helpful but unfortunatly im still getting the error :(/>
Edited on 05 February 2015 - 11:02 PM
asnarr204 #8
Posted 06 February 2015 - 01:33 AM
Should I report this as a computercraft bug?
Lyqyd #9
Posted 06 February 2015 - 02:30 AM
Which error are you still getting? If it's "Java Exception Thrown", it's likely a problem with the glasses bridge. If it's something else, please post the full text of the error message and the most recent version of the code.
asnarr204 #10
Posted 06 February 2015 - 02:43 AM
Which error are you still getting? If it's "Java Exception Thrown", it's likely a problem with the glasses bridge. If it's something else, please post the full text of the error message and the most recent version of the code.

It is "Java exception thrown" always now

What can I do to fix this if it is a problem with the bridge?
Edited on 06 February 2015 - 01:51 AM
Bomb Bloke #11
Posted 06 February 2015 - 03:35 AM
It'll most likely be throwing an exception because you're calling one of its functions incorrectly. Once upon a time we would've got semi-useful errors (albeit ones pointing to lines in the peripheral's Java code), but CC 1.6 put a stop to all that. For some reason.

So start out by narrowing the problem down. Distribute unique print statements (eg print("1"), print("2"), etc) throughout your script, and pay attention to which ones fire before the error occurs. You should be able to pinpoint the exact line which triggers the crash in this way.
asnarr204 #12
Posted 06 February 2015 - 11:54 AM
I am absolutely 100% sure the error occurs on the first if statement in mine() on line 131 because it says the mine function is starting with x iterations then stops. Also that is where the error message said something was wrong the first time, before it started saying java exception thrown.

It seems that i can perform math on commandNum and anything I set equal to it, but I cannot compare it to a constant.
Bomb Bloke #13
Posted 08 February 2015 - 06:09 AM
No, that won't be it. Add more print statements to further narrow it down.