Skip to content
Advertisement

Cannot invoke ” ” because array is null

I need to add information about 2 actors such as their name, address and age and have done so without arrays easily but it’s necessary, I keep getting the error

“Cannot invoke “TestActor.setName(String)” because “actors[0]” is null at TestMain.main(TestMain.java:5)”

This is just a test main I’m using to test it ”’

JavaScript

And this is the actor class I’m using that I need to set the information from

JavaScript

I know the toString doesn’t do anything there the way I have it setup just for the time being. It might be a bit of a mess and I might be in totally the wrong track. I was able to do it relatively easily wihtout using arrays but they’ve kinda stumped me and I’m not 100% sure on the direction to go in without maybe butchering the whole thing.

Advertisement

Answer

Compiler is complaining because you’re not initializing TestActor object correctly. You should rather do this:

JavaScript

If you don’t want to do this and use setters manually, then you need to define a default constructor in TestActor:

JavaScript

then you should be able to use it in your arrays like this:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement