File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes check equality of array string and string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "check equality of array string and string" Watch "check equality of array string and string" New topic
Author

check equality of array string and string

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I'm trying to compare a 2 dimensional array of Strings with a String variable and for some reason I get a null pointer exception when I use this method and I get error message at the line that contains if (rows[0][x].equals(code))


and this is the method I use to create an array from the CSV file


I declared private String[][] rows; at the top of my class containing these methods.
Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 171
    
    5

The null pointer exception is thrown when you try to call a method (equals) on a null value (rows[0][x]). Since null points to nothing, it can't execute any methods.

Where is rows instantiated? I see you're instantiating it in createArray(), but where is createArray() called from? The exception will be thrown if the array in general is null or the value at rows[0][x] is null.

Also, the size of the array defined in createArray() (i.e. rows[4][3]) and the number of iterations (0->5 = 4) don't match. If you were to get as far as accessing rows[0][4], you'd get an exception.


Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
How would rows[0][x] result in a null value, I have declared it in the same class as my createArray method which should have a value from the CSV file? createArray() is in the same class that item price is in.
Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 171
    
    5

I see you're instantiating it in createArray(), but where is createArray() called from?


Rows will be null until createArray() is called; because the code that instantiates it just hasn't been run. By default if you don't assign an Object a value, it is given a null value.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Ok I think I understand what your saying. I just need to call createArray() in itemPrice method and the values from the CSV file should be in the array. So how do I call a method to create an array? Would it be rows.createArray() or something like rows = createArray()?
Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 171
    
    5

Your createArray method already exists in the same class as your itemPrice method. Calling rows.createArray() would be incorrect because String[][] does not have a createArray() method. rows = createArray(rows) is closer but you need to watch the method signature; createArrays does not take any values as arguments.

Since rows is a global variable, you don't need to use the return value. Because rows is global the code will work if you write
(since both methods can access the rows variable) but it would be clearer to write .

This needs to be called before you go into the loop in itemPrice(...).
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I got it to work thank you.. but there were somethings I had to change to make it work which confuses me. I understand rows=createArray() but I had to change the size of the array to [5][4] instead of the [4][3]

There are 5 rows and 4 columns in my array and since arrays are counted from 0 why isn't it [4][3]? 0-4 and 0-3?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
You do not have an array of rows and columns; there is no such thing as a multi-dimensional array. What you have is a 5-element array whose members are 4-element arrays themselves.

newFoo[123][456]; means a 123-element array whose members are each 456-element arrays of Foo. Not a Foo array going up to index 456 (that would contain 457 elements).

The equals() and similar methods do not usually test for equality between Strings and String arrays, on the basis that a single String is going to be different from several Strings. An array is a different type of Object from a class instance. If you are testing equality between individual objects, you can try the equals() method with something to the left of it which is not null. So, "Campbell".equals(...); will happily accept anything as its argument. If you pass null, you simply get false back. If you pass something of a different class from String, you get false. If you pass a String which doesn’t read “Campbell”, you will also get false. You can of course test each array and (if the array isn’t null) its elements in turn.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Thank you for the help!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
You’re welcome
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: check equality of array string and string
 
Similar Threads
sorting an array from csv file.
Nested for loops to the point of confusion
problems creating a new larger array with one extra element
output newline to CSV file how to
null pointer