#NullPointerException for unknown reasons

67 messages · Page 1 of 1 (latest)

fringe willow
#

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.

marsh marlinBOT
#

This post has been reserved for your question.

Hey @fringe willow! Please use /close or the Close Post button 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.

hardy bay
#

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?

fringe willow
#

I didn't give the "MonkeyInfo" class because it's just data, that's not part of the issue

hardy bay
#

have you tried following the stacktrace to check what instance of Monkey is triggering the error?

#

to make sure it's that line

fringe willow
#

On it, one second

hardy bay
#

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

fringe willow
#

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

hardy bay
#

yeah

#

try debugging then

fringe willow
hardy bay
#

no, that's the opposite of what i said

#

you're catching an error, and then continuing as if everything's fine

fringe willow
#

Ohh you're right

#

How would I deal with that?

hardy bay
#

if Point or Monkey have adequate toStrings, try printing them out in select before the erroring line

hardy bay
fringe willow
hardy bay
fringe willow
#

Thanks

marsh marlinBOT
# fringe willow 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.

fringe willow
#

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

hardy bay
#

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

fringe willow
#

alright

hardy bay
#
this.position = // ...;
System.out.println(this);
//or
System.out.println(this.position);
fringe willow
hardy bay
#

thonk that isn't the point in your playLevel

fringe willow
#

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

fringe willow
#

place takes x=1053,y=1335 for the method where the issue is

hardy bay
#

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

fringe willow
#

Yeah I can, but i'd have to explain some names. But if that's fine for you, thanks for looking at it

marsh marlinBOT
hardy bay
#

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

marsh marlinBOT
fringe willow
fringe willow
fringe willow
#

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()

hardy bay
#

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?

fringe willow
#

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

hardy bay
#

check the line after the line mentioning select

fringe willow
#

Oh my god I found the issue

#

probably

fringe willow
# hardy bay 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

marsh marlinBOT
hardy bay
#

it happens, don't worry about it

#

make the mistake and learn a lesson about why good design is important 👍

fringe willow
#

It really is... But it's gonna be a bit until I can implement it. I can create working stuff, but not fix broken stuff

#

Thank you