• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

check equality of array string and string

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the help!
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
reply
    Bookmark Topic Watch Topic
  • New Topic