Hi, I have two relevant classes for this case: "Level" and "Monkey". Monkey has a "select()" method, that uses a Robot object to move the cursor to a certain point and executes other code after, that is irrelevant in that case. Each Monkey object has it's own Robot "bot" and it's own Point "position" The Monkey objects are not initializing their "position" upon construction. Only when monkeyObject.place(x,y) is called. The given x and y integers are the input values for the Point "position" objects for each monkey. To avoid a NullPointer I call "place(x,y)" every time before using "select()", yet I still get a NullPointer with the following explanation "Cannot read field x, because this.position is null". In my understanding this would only occur, if "position" wasn't initialized for "this", yet the "place" method always initializes "position". There is no if statement or anything to prevent that. I reduced the code to the chain of relevant parts. "Level" is the class i'm trying to write in and "Monkey" is the one I created to utilize in "Level". Does somebody know what I might be overlooking? I feel like i'm giving "position" a value no matter what, yet it tells me I don't.
#NullPointerException for unknown reasons
67 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @fringe willow! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
that isn't your code, that wouldn't be valid code in the body of Level
have you tried following the stacktrace to check what instance of Monkey is triggering the error?
Oh sorry, I totally forgot the method inside Level:
public class Level {
public static void playLevel() {
Monkey monkey = new Monkey(Monkey.dartMonkey);
monkey.place(782, 648);
monkey.select(); // throws exception in Monkey class
}
}
That's what I meant ^
I didn't give the "MonkeyInfo" class because it's just data, that's not part of the issue
have you tried following the stacktrace to check what instance of
Monkeyis triggering the error?
to make sure it's that line
On it, one second
wait, you aren't throwing in the Monkey constructor if bot isn't initialized?
even if that's not the current issue, that sounds like an issue waiting to happen
That's the stack trace I'd assume:
Exception in thread "main" java.lang.NullPointerException: Cannot read field "x" because "this.position" is null
at Monkey.select(Monkey.java:203)
at Level.playLevel(Level.java:195)
at Main.playLevel(Main.java:486)
at Main.main(Main.java:333)
Monkey 203 is the one i gave in "select()" bot.mouseMove(this.position.x, this.position.y;
Level 195 is where i call monkey.select()
and the two lines in Main just start the process
try {
bot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
``` wouldn't that be sufficient?
no, that's the opposite of what i said
you're catching an error, and then continuing as if everything's fine
if Point or Monkey have adequate toStrings, try printing them out in select before the erroring line
you could omit the catch and let it bubble up, or you could rethrow a more serious/different error, ie some subclass of RuntimeException or Error
Sorry i'm not that profound with properly dealing with errors. Would I just tell it to throw an exception and that's it?
i'd recommend you check this out https://dev.java/learn/exceptions
Thanks
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Alright one moment
I made a Monkey.toString(), that just outputs the position to string and the bot to string
@hardy bay So... expectedly I get this NullPointer Cannot invoke "java.awt.Point.toString()" because "this.position" is null
How did you want me to create toString again and what is its purpose in that case? I don't quite understand what you're going for yet
you don't need to call toString(), if you just try to print it directly it'll either print null or the toString
but well, it's clear that it's null now
try putting a print inside place, checking the same thing after the (supposed, i guess) assignment
alright
this.position = // ...;
System.out.println(this);
//or
System.out.println(this.position);
printing "this.position" gives
Monkey@71318ec4```
that isn't the point in your playLevel
that's the position where it's supposed to be and it's working as intended, yet the null pointer occurs when trying to access the variable
copy paste mistake.. my bad
place takes x=1053,y=1335 for the method where the issue is
could you copy paste your entire code then just remove unrelated sections? rather than copying out parts of it
(or just larger parts)
because im seeing a lot of inaccuracies and it's kinda hard for me to know what's really going on with them there
Yeah I can, but i'd have to explain some names. But if that's fine for you, thanks for looking at it
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
otherwise i'd say you should probably just do proper debugging, if it's too hard to separate out the relevant parts then it's likely also too hard for me to remotely debug 1 line at a time for you
It's not really optimized code and some stuff might be a little clunky. I was trying to get it to work
I do think I can separate the relevant parts easily. That's what I gave in the initial post. The other stuff works as intended, but I can still send it
I hope that's more understandable. I am working on a program to play a game automatically. All the stuff, that is called from "Main" is just constant values (mostly Color or Point objects), that contain data about the game's UI.
and most methods in general just involve placing the cursor at a certain position, clicking or waiting
What doesn't make sense to me is that the select() method from Monkey works for any other implemented level strategy. My program used to run smoothly, but when trying to implement a new strategy in Level in darkDungeons() it fails. I have about 8 other methods with the same structure as darkDungeons() (which contrains the strategy operations) that work as expected, all while using select()
so a btd6 clone? not placing the monkeys as soon as you construct them just seems like a recipe for disaster here
did you trace the stacktrace back to which call is throwing, and then check if that monkey has been placed?
It's an autoplayer for the game, using certain strategies for certain maps to farm stuff. I'll see why I didn't incoperate the placing into the object creation. There was a reason for it, 1 sec
I honestly don't know why I didn't use place directly inside the constructor. I think my thought was to put operations like "select, upgrade or place" on one level, that they are just called in order inside Level
How would I do that?
check the line after the line mentioning select
I am placing dartMonkey two times... The issue occurs at dartMonkey2.select() in Level.darkDungeons(). I am never calling dartMonkey2.place(x,y) therefore there is no position to extract, when trying to select. You were absolutely right, not filling "position" when the object is created sets up disaster.
That's why it's working flawlessly for other strats
@hardy bay Thanks for the effort. Sorry for coming with such stupid mistake on my side. I appreciate the patience
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.