• 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

Why does my two-dimensional array not accepting any values?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm creating a 2D array program that accepts values from users and to perform two operations, such as Adding and Multiplying the array, then print the results. However, the program is not accepting any values, it prints the following error:

Exception in thread "main" java.lang.NullPointerException
at twodimensionalarray.Matrix.getMatrix(Matrix.java:27)
at twodimensionalarray.TwoDimensionalArray.main(TwoDimensionalArray.java:18)

 
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At first glance: you are initializing Matrices often with: new Matrix(); but that will give you a 0x0 Matrix.
 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Beware of multiple constructors. I think every class should have a constructor, (even a private constructor, which can be very useful for reasons explained here), but multiple constructors give you multiple opportunities for something to go wring, I am afraid. Your two‑argument constructor, for example, doesn't initialise the Array field (which should be called array). So any use of that field won't work. It will be null and you will have this sort of exception thrown.
Only call your methods getXXX() if they return something. I am afraid I can see another problem with that method. You correct the problems with the constructors and they run correctly, but there is another enhancement you should make which I am keeping quiet about at the moment. Now there is no way to guarantee that the method to fill the matrix is ever called. I suggest you call that method from the constructor. Beware: methods called from the constructor should be marked private or final. There is no need to use both modifiers.
Your printf call in line 35 won't work, I am afraid. Get rid of the +, and I suggest you change the numbers after % too. System.out.printf("%6.2f, ", array[i][j]); will give you 8 characters per number, like this -12.34, .

Don't copy'n'paste any of this post because it may contain hard space characters.
 
Joshua Adams
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the information guys, I got the program to work!!
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome!

Petite question: why are you casting the elements to an int in 'addMartix' and 'multiplyMartix'?
 
Marshal
Posts: 28193
95
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
"addMartix"???
 
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
Martix is what we used to call one of the mods. You remember him, surely?
 
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
I have found another error in your no‑arguments constructor.
 
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

Norm Radder wrote:Also posted here:. . .

Thank you for noticing I must say the link about cross‑posting on that other website is very useful.
 
Paul Clapham
Marshal
Posts: 28193
95
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

Campbell Ritchie wrote:Martix is what we used to call one of the mods. You remember him, surely?



No, he was one of the characters in the Asterix canon. The one with the stalls full of vegetables and fruits, I think.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic