• Post Reply Bookmark Topic Watch Topic
  • New Topic

I can't find the error  RSS feed

 
Alex Garci
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have the following code:



The rest of classes is here:







When I run the code I display the following error:

Exception in thread "main" java.lang.NullPointerException
at Anadeobjetosalalistamultimedia.main(Anadeobjetosalalistamultimedia.java:18)
Java Result: 1

After reading some information about this error in Internet I only know the error is due to a null reference in the code. But I don't understand the meaning of this.

How can I fix it?

Thanks.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In line 7 of Anadeobjetosalalistamultimedia you are creating a new array with elements of type EstanteriaMultimedia, with 20 elements.

But you are not initializing the content of the array anywhere. So the array will contain 20 null references. This is a mistake that many beginners make with arrays.

In line 18 you're trying to call a method on e[0], but e[0] is null so you get a NullPointerException.

You need to fill the array with EstanteriaMultimedia objects somewhere.
 
Sunil Sunny
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
initialize you array as e[0]=new EstanteriaMultimedia(1) or whatever parameter you want to give
 
Alex Garci
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I initialized array EstanteriaMultimedia but I displayed the same error. But I not initialized EstanteriaMultimedia array, I also did the same in listamultimedia array. But the NullPointer exception error didn't disappear.

The code I modified:

Class Anadeobjetosalalistamultimedia





Class EstanteriaMultimedia



What's wrong?

Thanks.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't solved the problem yet. Look at your code:

Note that in line 13 and 14, e[i] is null. You haven't created a new EstanteriaMultimedia object yet and assigned it to e[i].
 
Alex Garci
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have initialized array e[i] typing the following:

e[i]=new EstanteriaMultimedia(20);

But the NullPointerexception error was displayed in class EstanteriaMultimedia. I typed something similar as on the class Anadeobjetosalalistamultimedia:

listamultimedia[i]=new Multimedia[20];

When I thought the error was fixed, the java compiler throws the following error:

incompatible types

required Multimedia
found Multimedia[]

Here the new code:

class EstanteriaMultimedia



class Multimedia



class Anadeobjetosalalistamultimedia



How can I fix it?

Thanks.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Garci wrote:
I have initialized array e[i] typing the following:

e[i]=new EstanteriaMultimedia(20);

But the NullPointerexception error was displayed in class EstanteriaMultimedia.


Basically, you fixed the problem, only to reveal a different problem in a different location. As for how to fix it, it is basically the same issue, and the fix is similar to the earlier one -- only for different types.


Alex Garci wrote:
I typed something similar as on the class Anadeobjetosalalistamultimedia:

listamultimedia[i]=new Multimedia[20];

When I thought the error was fixed, the java compiler throws the following error:

incompatible types

required Multimedia
found Multimedia[]


Don't know why you decided to do a different fix -- but obviously, you can assign an array to an non-array, and the compiler is telling you that.

Henry
 
Alex Garci
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying. The issue is solved now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!