• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Query Regarding Null Pointer Exception While trying to initialize an element of an array

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am getting a java.lang.NullPointerException if i do the following:

//Code snippet starts
double [] [] myMatrix=new myMatrix[3][];
myMatrix[0][0]=1.2D;//this is the line which raises the java.lang.NullPointerException
//Code snippet ends

Can anyone tell me what's wrong with the above code?

Thanks.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Azeem Mohammed wrote:double [] [] myMatrix=new myMatrix[3][];


That is not going to work. It needs to be double[][] myMatrix = new double[3][];

Azeem Mohammed wrote:
myMatrix[0][0]=1.2D;//this is the line which raises the java.lang.NullPointerException


That is because you didn't initialize the array: myMaxtrix[0] = new double[3];
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to initialize the individual arrays of myMatrix.

Update: Had typed this, but was really late to post the reply . Wouter has a more detailed reply
 
Sheriff
Posts: 22683
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
new double[3][] creates an array of double[]. All these elements are still null. You need to initialize them like Wouter suggested. If you provide both dimensions when creating, new double[3][3], then all the elements of the outer array are also initialized.
 
Marshal
Posts: 76450
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohamed Sanaulla wrote: . . . Update: Had typed this, but was really late to post the reply . . .

You have come across the term "race condition" in multi-threading? Well, these discussions are called threads, and you and Wouter were multi-threading in a race condition This time, Wouter won. Next time, good luck to you
 
Greenhorn
Posts: 14
PHP Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Array declaration is not correct. mention the data type in right side with no of elements

 
Azeem Mohammed
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot everyone.
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic