This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Understanding what to do with null pointer exception  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I am learning JavaFX by going through the JavaFX for dummies book (very good IMO) and I am at the chapter covering tables in JavaFX.  There is a sample project which adds, changes and deletes Movie records in an observable list.  Using this program as  base, I am writing a check book program to add, change and delete checks and deposits.  My Add check routine and Delete check routine give me null pointer exceptions when I try to run them.

Here is my code to add a check:



Here is my code to create the Checks Object:


My Checks table loads fine and I can view the entries without errors.

What is frustrating is that I am not making any major changes to the code given in the book; just changing the field names.

Let me know if you need any other parts of my program.

Thanks!
Tom Nesler

 
author & internet detective
Posts: 39281
727
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which line throws an exception?
 
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't dal with null pointer exceptions; yoiu need to deal with the nulls in your program. In this case it is likely that something is pointing to null, which should point to a “real object”.
Don't use double arithmetic for money; use BigDecimal.
I don't like the name of your method; it should read writeCheque() or something like that.
 
Sheriff
Posts: 13453
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The most likely suspect is the table variable that you reference on line 8. If the second snippet of code you gave is inside a different method, then the table variable is local to that method. If you also have a table instance variable that the btnAddChk_Clicked() method can access, then that variable will not have been initialized if the only place you think you're doing that is with the second snippet.

Your names are very poorly chosen. They have the following smells:

1. They are not readable and are difficult to pronounce. Prefer spelling out words in names. That names easier to read both silently and out loud. You should try to read code out loud, even when you're the only one reading it. There's something about reading code out loud that helps us find problems more easily than if we just silently read it to ourselves.

What's easier to read, "Tm Nslr" or "Tom Nesler"? Some of the abbreviations are pretty standard, like "btn" and "txt" so I can let those slide. If you must use abbreviations like this in your names, then make that the only abbreviation you use. Don't use multiple abbreviations. The "chk" abbreviation should be spelled out. Don't discriminate against vowels and digraphs (two consonants that spell one sound); they are letters, too, you know.

2. They are focused on implementation. Good names are not "leaky," meaning they don't say (leak information) about the underlying implementation.

Instead, good names reveal the intent or purpose of the thing they represent. The name table, for example, is purely about implementation. What does that table contain? If the table contains Check objects, then at the very least, the name should contain the word "check" in it. I would go with something like checks because it implies a collection of Check of some kind without saying anything about the underlying implementation, whether that be a table or a List or array or whatever kind of container you may have.

Good names that focus on intent/purpose play a critical role in the quality of your code. They make your code more readable and easier to understand, thus, easier to work with. They keep your code robust by being more resilient to changes in implementation. Things that are focused on implementation become liars and deceivers when the underlying implementation changes. Look at the comments you copied. When you changed the names from "movie" to "check" you made that comment a liar and deceiver because it says "Movie" instead of "Check" -- anyone reading that comment will get confused, just as I was.

 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to what Junilu said about that comment: if you make the name of the method clear and self‑explanatory, the comment becomes unnecessary. If the method were called addNewCheque(), that would render such a comment unnecessary.
 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the fact that I have never fallen under the baleful effects of Noah Webster, I wouldn't call a class Cheques. If it represents a cheque, it should be called Cheque not Cheques.
 
Junilu Lacar
Sheriff
Posts: 13453
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If it represents a cheque, it should be called Cheque not Cheques.


Sure, but if the name represents some kind of collection of checks (/toh-MAY-toe/ /tuh-MAH-toh/), then you probably should go with the plural form so it's not misleading.
 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . /toh-MAY-toe/ /tuh-MAH-toh/),

That's one we can't blame Webster for

then you probably should go with the plural form so it's not misleading.

Good point.
 
Tom Nesler
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky and Juniilu Lacar:  You are correct Line 8 is where the exception comes from.

8   table.getItems().add(ch);  



I agree that scoping is probably the issue.  I create the Checks object outside of the Javafx Start method.



Would moving this code up to where I create the table help?

1 TableView<Checks> table =  new TableView<Checks>();            //Construct Table object
2  table.setEditable(true);
3  table.setItems(loadData());                                  //Add rows using LoadData method  



The "LoadData" method is outside of the Start method and it does not cause an exception.

Juniilu Lacar: Sorry for the confusion.  The comment was copied along with the code I got from the Move Table book example.
 
Marshal
Posts: 5980
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I agree that scoping is probably the issue.  I create the Checks object outside of the Javafx Start method.

Would moving this code up to where I create the table help?


You should have a line something like this in your code, outside of any method and usually at the top:
Then here...
... remove the declaration of the table variable:
Now you're using the instance variable instead of hiding it with a new local variable.
 
Tom Nesler
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That fixed the problem!...Yaay!

Looking at the example in the book, that's how they did it as well.Imust have gotten confused when I copied the program.

Thank you everyone!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!