• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Exception in thread "main" java.lang.NullPointerException

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not the whole program. But basically the program takes a <path> tag (or multiple path tags) from a scalable vector graphic file and converts it to a string and then removes n% unimportant points that the user decides.
Thanks to all those who can help!

This is the error that I am receiving when I go to test my code with an actual SVG file:

Exception in thread "main" java.lang.NullPointerException
at SVG.<init>(SVG.java:54)
at EditSVG.main(EditSVG.java:8)


I did some research and found that this error is caused by probably one of these three:
- Attempting to invoke an instance method of a null object
- Attempting to access or modify a particular field of a null object
- Attempting to obtain the length of such null object as an array

I have failed to locate the problem. Any suggestions on where in this code I can look for the problem and possibly a hint on what the specific problem is?

Here is a code snippet that ends on line 54 of my SVG.java file



Here is a code snippet from my EditSVG.java:




Thanks again - Will>
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If this line is causing the exception:


then Paths is null, since that's the only reference you dereference in that line.

As a side note, Java convention is to begin variable names with a lowercase letter.
 
William Koch
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If Paths is null why can I not add something to it. I declared Paths in line 6. Is there a simple fix? Do I need to give Paths some initial value?

thanks for the naming convention tip! :-)
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

William Koch wrote:If Paths is null why can I not add something to it.



Because it's null. The Paths variable holds a reference whose value is null--that is, a reference that doesn't point to any object. You have to create your ArrayList object before you can call methods on it. You can't call methods on a null reference.

I declared Paths in line 6.



Declaring a variable != creating an object.

Is there a simple fix?



Yup.

Do I need to give Paths some initial value?



You have to give it a non-null value before you dereference it. The most appropriate place in this case is probably the line where you declare it, but it doesn't have to be there.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
William Koch
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does this look?



I am new to Java if you haven't noticed. My first language was Ada95 but now I am having to learn Java as they try to teach me data structures. I am in my second programming course for my undergrad in Computer Science (it is a Data Structures course).

I appreciate all the help you give here at The Ranch

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well you still have a variable name starting with an upper case letter so on first inspection it looks odd.

BTW The question should be not "How does it look" but "Has it solved the problem", I take it you have compiled and run the code to see.
 
Marshal
Posts: 27371
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:Well you still have a variable name starting with an upper case letter so on first inspection it looks odd.



Especially as it's the same as the name of a class in the standard Java API: java.nio.file.Paths.
 
William Koch
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, It still does look odd and I will begin fix it for future post. It did work! So thank you.

Especially as it's the same as the name of a class in the standard Java API: java.nio.file.Paths.



I do not believe that is currently affecting my program but yes I should not use the names of classes. I will work on these. Thanks a bunch.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I do not believe that is currently affecting my program


You are correct, but it is confusing to people who didn't write the code (and probably will be to you in a few months time).

Coding standards are there to help ensure everyone can easily understand the code you have written which is important especially when you asking others for help with your code.
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic