2

I tried to fix all but still gives same error. Here is code, when I try to add an instance to vector that's happened. any idea?

private static Vector<DrawingElement> vector;

public void Add(DrawingElement de) {    
        vector.addElement(de);   // ERROR  //
        System.out.println("ADDED!");       
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
blackman
  • 171
  • 1
  • 2
  • 11

6 Answers6

2

You have to initialize your Vector with:

private static Vector vector = new Vector();

to avoid the NullPointerException. Once initialized, you'll be able to add elements.

eternay
  • 3,754
  • 2
  • 29
  • 27
1

it seems the vector variable is not initialized. that's why you are getting a NullPointerException.

you have to initialize the vector. for example

Vector vector = new Vector();

or

Vector vector = new Vector(size);  // with the capacity

in your case you are adding DrawingElement class in vector so you have to initialize something like this

Vector<DrawingElement> vector = new Vector<DrawingElement>();
stinepike
  • 54,068
  • 14
  • 92
  • 112
0

Make sure you initialized the Vector like so:

private static Vector vector = new Vector();

Even better, you might want to create your Vector as generic if it will only store objects of type DrawingElement:

private static Vector<DrawingElement> vector = new Vector<>();
Jurgen Camilleri
  • 3,559
  • 20
  • 45
0

In java You have to verify the object is not null before doing any operations over it. If you are trying to refer an object that is not created by using new operator, you will get NullPointerException

the solution for your problem would be as simple as below

    String s1 = "sample string1";
    String s2 = "sample string2";
    Vector<String> v = new Vector<String>();
    v.add(s1);
    v.add(s2);
    Iterator<String> i = v.iterator();
    while(i.hasNext()){
        System.out.println(i.next());
    }
MaheshVarma
  • 2,081
  • 7
  • 35
  • 58
0

The idea is the object might not yet been initialized. Use the code to fix error

public void add(DrawingElement de) throws IllegalStateException { 
     if (vector != null) {  
        vector.addElement(de);   // OK  //
        System.out.println("ADDED!");  
     } else {
        System.out.println("ERROR?!"); // ERROR  //
        throw new IllegalStateException("ERROR object not initialized");
     }

}
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • thanks guys, ıt was sipmle but make exhausted me ! No tricky way, do it in a simple way. The simplest is the hardest by the way... – blackman May 31 '13 at 14:34
  • @blackman I guessed that the object was lazy initialized, sorry if it couldn't help. – Roman C May 31 '13 at 14:41
0

It is very simple to solve. your code

private static Vector<DrawingElement> vector;

public void Add(Vector<String> de) {    
        vector.addElement(de);   // ERROR  //
        System.out.println("ADDED!");       
}

my code

    vector <String> vector= new Vector();
    public void Add(DrawingElement de) {  
    vector.add(de);

}
BIS Tech
  • 17,000
  • 12
  • 99
  • 148