#java.lang.NullPointerException

1 messages · Page 1 of 1 (latest)

meager radish
#

Been getting this issue with my code, Im creatint a game using processing and Ive tried debugging it, im a beginner so idk how im supposed to fix it or where is causing the issue

i think this is the line causing the issue

        try {
            // System.out.println("Half of setup :D");
            board.loadLevel(this.getClass().getResource("level1.txt").getPath().toLowerCase().replace("%20", " ")); 
            // System.out.println("this prints :D");
        } catch (IOException e) { 
            System.out.println("not working :(");
            e.printStackTrace();
        }

Help 😦

lone lichenBOT
# meager radish Been getting this issue with my code, Im creatint a game using processing and Iv...

Detected code, here are some useful tools:

[WARNING] The code couldn't end properly...

Problematic source code:

try {
            // System.out.println("Half of setup :D");
            board.loadLevel(this.getClass().getResource("level1.txt").getPath().toLowerCase().replace("%20", " ")); 
            // System.out.println("this prints :D");
        } catch (IOException e) { 
            System.out.println("not working :(");
            e.printStackTrace();
        }```
Cause:
The code doesn't compile:
cannot find symbol
  symbol:   variable board
  location: class 
non-static variable this cannot be referenced from a static context
cannot find symbol
  symbol:   class IOException
  location: class 

## System out
[Nothing]
#

<@&987246399047479336> please have a look, thanks.

surreal lava
#

Provide the stack trace. It points to the exact line where the exception occurred

meager radish
#

Line 62

surreal lava
#

I suspect the issue comes from getClass().getResource("level1.txt")

meager radish
#

Can provide a bigger snippet if needed

alpine acorn
#

If you place a breakpoint you can debug it.

#

For example is your board not null?

meager radish
#

How do i do that

alpine acorn
#

It depends on your IDE.

meager radish
#
this.getClass().getResource("level1.txt").getPath(), StandardCharsets.UTF_8.name())
``` tried this as well and doesnt work
meager radish
#

Im using java 8 on vsc for context

#

processing papplet

surreal lava
#

You can't bruteforce a solution, it's not how you are supposed to write code

alpine acorn
#

I recommend using intelliJ, it has better debugging.

#

And java 9 and later have better nullpointerexceptions.

#

At first glance your file isn't found.

meager radish
#

Do you know what possibly could be the issue tho?

surreal lava
#

Depending on where your file is located, you might need to use another matter of loading a file. If it's located in the project's root directory, then Path.of("level1.txt") would be sufficient.

alpine acorn
#

So dependant on your project setup it might not be detected as a resource folder.

meager radish
#

its inside a resources folder

alpine acorn
#

src/main/resources?

meager radish
#

It detects my other sprites tho

#

No

#

src/main/inkball/resourses

#
    public void loadLevel(String filePath) throws IOException {
        System.out.println("working :D");

the print line doesnt even run when i call this using the initial issue as well

#

so im suspecting its the way i grab for the file

surreal lava
#

Are you building your project using maven/gradle? Behavior may vary from a tool to another tool

meager radish
#

gradle

alpine acorn
meager radish
#

Ye but u need to catch that just incase no?

alpine acorn
#

If you were to put Exception ex there you would see the exact exception.

meager radish
#

Otherwise u get an error

alpine acorn
meager radish
#

Oh yea

alpine acorn
#

Since nullpointers are runtime exceptions.

surreal lava
# meager radish gradle

Right, so it should be under src/main/resources. If other resources in the same directory are loaded fine, then that particular file might have not been copied during the build

meager radish
#

should i also make an exception

surreal lava
#

You better familiarize yourself with a debugger. It will save you a lot of time.

meager radish
#

What does that mean

meager radish
meager radish
meager radish
alpine acorn
surreal lava
meager radish
raw heath
# meager radish yep

a few problems

  • if you have src/main/resources/Level1.txt then the path should be /Level1.txt
  • don't use regular io to load stuff from resources, you can't, you have to use getResource or getResourceAsStream. I repeat, File, Path and Files won't work.
  • processing isn't compatible with resources so depending of what you need to load, you can't use resources

and also, please show loadLevel

meager radish
#
    public void loadLevel(String filePath) throws IOException {
        System.out.println("working :D");
        Level_loader level_loader = new Level_loader(p.loadJSONObject("config.json"), spriteManager);
        System.out.println("still working :D");
        level = level_loader.loadLevel(filePath);
        System.out.println("even more working :D");
    }
surreal lava
meager radish
#
  public Board(PApplet p, SpriteManager spriteManager) {
        this.level = null;
        this.p = p;
        this.spriteManager = spriteManager;
        this.score = 0;
        this.isPaused = false;

    }
raw heath
#

right so that's processing

meager radish
#

Mhmm

raw heath
#

so you can't use processing with resources

#

instead put them in a data folder outside of src

#

it should work

meager radish
#

Wdym?

surreal lava
raw heath
meager radish
#

Coz like i have other resources sucha as sprites n .json that work completely fine tho

meager radish
raw heath
meager radish
#

why tho

raw heath
meager radish
#

Ill give u an example sec

raw heath
meager radish
#
    public PImage getSprite(String s) {
            PImage result = sprites.get(s);
            if (result == null) {
                result = loadImage(this.getClass().getResource(s + ".png").getPath().toLowerCase().replace("%20", " ")); 
                sprites.put(s, result);
            }
            return result;
        }

can u explain why this works and not the board one? arent they kinda similar in a way tho?

raw heath
meager radish
#

is it not supposed to work?

#

does it just not work with txt files

raw heath
meager radish
#

Can i make that miricle happen again then or?

raw heath
raw heath
meager radish
#

Is it supposed to be a local file

raw heath
#

you are abusing features in unintented ways

#

and it may break at any moment

meager radish
raw heath
meager radish
#

Move it outside my src?

#

how do i call it then

raw heath
#
  • if you have src/main/resources/Level1.txt then the path should be /Level1.txt
  • don't use regular io to load stuff from resources. You can't. You have to use getResource or getResourceAsStream. I repeat, File, Path and Files won't work.
  • processing isn't compatible with resources, so depending of what you need to load, you can't use resources, you have to create a data folder outside of src
meager radish
#
board.loadLevel(this.getClass().getResource("level1.txt").getPath().toLowerCase().replace("%20", " ")); 

i am usig getresources here tho

raw heath
#

I have three points

#

you broke all three

meager radish
#

So im missing a / for the first one

#

2nd one?

#

I get the 3rd one

raw heath
meager radish
#

No clue

raw heath
#

mouse over getPath

meager radish
#

is it returning null?

raw heath
#

Ah my bad

#

Let me fix my message

meager radish
#

Java hurts my brain 😦

raw heath
#
  • if you have src/main/resources/Level1.txt then the path should be /Level1.txt
  • don't use regular io to load stuff from resources. You can't. You have to use getResource or getResourceAsStream. I repeat, File, Path and Files won't work. Ah and you can't use raw strings either.
  • processing isn't compatible with resources, so depending of what you need to load, you can't use resources, you have to create a data folder outside of src
meager radish
#

Do i need a file getter or soemthing as well?

raw heath
#

no

#

you can't use regular io

#

you have to use getResource or simpler, getResourceAsStream

meager radish
#

Im still not understanding, because i am using getResource here tho

#
this.getClass().getResource("/level1.txt").getPath().toLowerCase().replace("%20", " ")```
raw heath
meager radish
#

There is getResource

raw heath
#

you are supposed to only use getResource

#

nothing else

meager radish
#

Ok ok

raw heath
#

(well, uri also works I guess)

meager radish
#

this.getResource("/level1.txt").getPath().toLowerCase().replace("%20", " ")?

#

like this or smth?

raw heath
#

you are still doing the same thing

#

you can only use getResource or getresourceAsStream

#

nothing else

meager radish
#

so the updated would be

            // System.out.println("Half of setup :D");
            board.loadLevel(.getResource("/level1.txt")); 
            // System.out.println("this prints :D");
        } catch (IOException e) { 
            System.out.println("not working :(");
            e.printStackTrace();
        }
#

Yes?

raw heath
#

i'm telling you

meager radish
#

What am i doing wrong now??

raw heath
#

you can't do that

raw heath
#

you can't put a .something out of nowhere

meager radish
#

o lol woops

raw heath
#

anyway

#

forget about getResource

meager radish
#

im getting this anyways

raw heath
#

usually you would only use getResourceAsStream

#

well yes

meager radish
raw heath
#

but

meager radish
#

Ill google it...

raw heath
#

please

#

let me explain

meager radish
#

ok ok

#

my listening ears are open

raw heath
#

usually you would only use getResourceAsStream

#

forget about getResource

#

but processing is a particular case

#

you can't use resources at the first place

#

and so this is where my 3rd point comes

#

you have to add a data folder above src

meager radish
#

Yep

raw heath
#

and use a relative path

meager radish
#

For most things

raw heath
#

whenever processing asks you for a path

#

put the file in data

#

and give the file name as the path

meager radish
#

ok ok

#

is that why my images work fine?

raw heath
# meager radish is that why my images work fine?

the reason why your messages work fine is because you followed point 1 correctly, compared to here
But like I said, this is a coincidence, the reason why it works fine even if you are using a resource is because you are using regular io on an absolute path.
But those resources can be transformed depending the situation. For example, if you try to make a jar, it would totally break, because regular io can't handle files inside a jar. But getResource and getResourceAsStream can

meager radish
#

Hmm so I should be fine using either of them from now on?

#

Great thanks

raw heath