Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getting null pointer exception! how to fix it.  RSS feed

 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is a text file having data in rows and column with positoning the tabs and \n . i am getting all rows. now the first element in row is a primary key in a (DB) table. and i want to do the operations on the other column.

Problem is that when i retrives a row, at last its showing null charecter or space type.



now for 124 id. its primary key in a table master table. and other column in mastertable having primary key 124 is its name.

Am splitng a row on "\t" string and store in String array
now it gives total 6 values in string array 123, 4, 30, no$, 0 and last is null i.e. nothing

my code is

DefaultSupString uses searchrecord class


-------------
Exception

java.lang.NullPointerException
at supstring.SearchRecord.getRecordSet(SearchRecord.java:37)
at supstring.DefaultSupString.itemaspv(DefaultSupString.java:24)
at supstring.DefaultSupString.main(DefaultSupString.java:58)

[ UD: added CODE tags ]
[ July 14, 2007: Message edited by: Ulf Dittmer ]
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MORE elaborate about my test file presentation

my_text_file.txt
-------------------------------------------------------
| mid pid pv vt Ref FD TD
| 123 9 25 no$ 0
| 124 9 25 no$ 0
| 125 9 25 no$ 0
| :
| :
| :
|

Problem: I am storing one row in string array
i.e. String arr2[]={123,9,25,no$,0, , ,}.There is nothing in column FD & TD

Now, how can avoid vlaues of FD TD to eliminate NullpointerException.?

its giving exception in file Searchrecord.java file
at
if (temp==9){
dss.getSupplierString(arr2); // here am getting problem

}

please tell me . i am in need.
[ July 14, 2007: Message edited by: Rahul Shilpakar ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you just need to check each array entry for null before using it:

Depending on your business rules, you can sometimes plug in a default value. Right after your split() you might fill in defaults:

This is probably outsid ethe bounds for the beginner forum, but some times we can provide a special object that defines the behavior for missing data.

That can avoid repetitive tests and lame comments in later code.

Any of those ideas work for you?
[ July 15, 2007: Message edited by: Stan James ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!