Why does the following multidimensional array in Java throw a null pointer exception?
-
int[][] mdArray = new int[5][]; int[][] msArray = new int[4][]; int[] singleDArray = {1,2,3,4,5}; int[] singleDArray2 = {6,7,8,9,}; mdArray[4] = singleDArray; msArray[3] = singleDArray2; System.out.println("Before assigning multi dimensional mdArray Array" + mdArray[4][0]); System.out.println("Before assigning multi dimensional msArray Array" + msArray[3][0]); msArray = mdArray; System.out.println("Before assigning multi dimensional mdArray Array" + mdArray[4][0]); System.out.println("Before assigning multi dimensional msArray Array" + msArray[3][0]); // This line throws null pointer exception and I have no clue. Please help me.
-
Answer:
You set mdArray[4], but mdArray[3] is null. So when you set msArray = mdArray and try to access msArray[3], nothing is there.
William Oliver at Quora Visit the source
Related Q & A:
- How to find the column name of the first column with a null value?Best solution by Stack Overflow
- How to hashMap an array in Java?Best solution by Stack Overflow
- How do I throw a real party?Best solution by Yahoo! Answers
- How do I throw a great 4th of July party?Best solution by Yahoo! Answers
- How would you throw a party?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.