-1

I'm trying to create a little project of mine, and I created an array of an element. It however creates a NullPointerException when executed.

package main;
import java.io.*;
import java.util.Arrays;
public class item implements java.io.Serializable{

public String Name;
public String Description;
public float[] Stat;

public static void main(String [] args)
   {
      item Items[] = new item[1000];

      Items[0].Name = "item1";
      Items[1].Name = "item2";

      try
      {
         FileOutputStream fileOut = new FileOutputStream("../items.config");
         ObjectOutputStream out = new ObjectOutputStream(fileOut);
         out.writeObject(Items[1]);
         out.writeObject(Items[0]);
         out.close();
         fileOut.close();
      }catch(IOException i)
      {
          i.printStackTrace();
      }
   }

}

How do I initialize the element to remove the NullPointerException It occurs on lines 13 & 14

  • The null pointer exception over there, clearly indicates that the files could not be located. – Panda Mar 04 '16 at 02:46
  • 2
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – John3136 Mar 04 '16 at 02:48

2 Answers2

0

You need to instantiate the element in array.

Your code must be like this

Items[0] = new item();    
Items[0].Name = "item1";
Items[1] = new item();    
Items[1].Name = "item2";
josivan
  • 1,963
  • 1
  • 15
  • 26
0

Objects need to be instantiated using new before you can operate on them.

// Instantiate objects of class "item"
Items[0] = new item();
Items[1] = new item();
ahe
  • 227
  • 1
  • 7