88997

How to return an array object without NullPointerException?

Question:

Declared:

private Man[] man;

This is the initialization:

Man[] man = new Man[1]; for (int i = 0; i < 1; i++){ man[i] = new Man(); for (int j = 0; j < 3; j++){ man[i].eatThis(table.foods[table.topFood-1]); table.topFood--; } }

Want to print this:

System.out.println(getMan(0));

which goes to:

public Man getMan(int k){ return man[k]; }

but I receive NullPointerException. Why? While:

System.out.println(man[0]);

works just fine.

Exception in thread "main" java.lang.NullPointerException at ManRunning.getMan(ManRunning.java:80) at ManRunning.newGame(ManRunning.java:133) at ManRunning.<init>(ManRunning.java:57) at RunDevilRun.main(RunDevilRun.java:9)

Answer1:

the line (1)

Man[] man = new Man[1];

is hiding the instance variable declared in this line (2)

private Man[] man;

any decent IDE would show a warning for this.

here is how you should initialize the array man in the line (1) declared with line (2)

man = new Man[1];

Answer2:

Obivously you have two man array variables, one that is initialized and one (a member variable) that isn't.

Recommend

  • read values from form post in jquery or javascript
  • apply a javascript function to draggable copy
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • Exception gevent.hub.LoopExit: LoopExit('This operation would block forever',)
  • Group list of tuples by item
  • Reading a file into a multidimensional array
  • Cannot upload to OneDrive using the new SDK
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • App restarts from wrong activity
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Jetty Server not starting: Unable to establish loopback connection
  • Lost migrations and Azure database is now out of sync
  • Get one-time binding to work for ng-if
  • Dynamically accessing properties of knockoutjs observable array
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Spray.io: When (not) to use non-blocking route handling?
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • javascript inside java/jsp code
  • sending/ receiving email in Java
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • How can I estimate amount of memory left with calling System.gc()?
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Android Studio and gradle
  • Proper folder structure for lots of source files
  • Hits per day in Google Big Query
  • JTable with a ScrollPane misbehaving
  • How does Linux kernel interrupt the application?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • unknown Exception android
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?