Προς το περιεχόμενο

Dynamic Array: java.lang.NullPointerException κατά την προσθήκη στοιχείων


SpirosG86

Προτεινόμενες αναρτήσεις

Δημοσ.

Καλησπέρα! Πάλι εγώ :-(

 

Έχω δημιουργήσει μια dynamic array θέλοντας να αντιγράφω συγκεκριμένα υπό συνθήκη στοιχεία μιας static.

Τον κώδικα για την dynamic array τον έχω βρει από το νετ και περιέχει την μέθοδο put(int position, int value) που εισάγει το στοιχείο value στη θέση position.

 

Προσπαθώντας όμως να υλοποιήσω τα παραπάνω, η κονσόλα μου πετάει το εξής μήνυμα:

 

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at putClippedPolygon.<init>(putClippedPolygon.java:52)

 

Ο υπόλοιπος κώδικας δεν ευθύνεται(τον είχα δοκιμάσει με static arrays). Ξέρει κανείς τί μπορεί να φταίει;

Βρήκα στο νετ πάλι τα εξής για το παραπάνω σφάλμα:

"The most likely cause is when the application attempts to invoke a method on an object that it believes has been instantiated, but that is, in fact, null. Applications developed in C or C++ (and other languages) would trap with a segmentation violation in this case, but the Java runtime detects the null object and throws java.lang.NullPointerException instead. Other reasons this exception might be raised include the following:

 

* Accessing attributes (such as, instance variables) on the null object;

* Attempting to use the null object as if it were an array;

* Throwing a null object as if it were an instance of java.lang.Throwable."

ωστόσο δεν κατάλαβα και πολλά... Μπορείτε να με βοηθήσετε;

 

Η κλήση γίνεται με αυτόν τον τρόπο:

>			ClippedPolygonX.put(0, polXCoord[0]);

 

Ο κώδικας της μεθόδου put(int position, int value):

>       public void put(int position, int value) {
            // Store the value in the specified position in the array.
            // The data array will increase in size to include this
            // position, if necessary.
         if (position >= data.length) {
                // The specified position is outside the actual size of
                // the data array.  Double the size, or if that still does
                // not include the specified position, set the new size
                // to position + 1.
            int newSize = data.length + 1;
            if (position >= newSize)
               newSize = position + 1;
            int[] newData = new int[newSize];
            System.arraycopy(data, 0, newData, 0, data.length);
            data = newData;
         }
         data[position] = value;
      }

Δημοσ.

Οκ το βρήκα, ήταν το

"The most likely cause is when the application attempts to invoke a method on an object that it believes has been instantiated, but that is, in fact, null.".

Έπρεπε όπως φαίνεται να ορίσω και στη μέθοδο και στην κλάση τις Arrays... όπως και να χει ευχαριστώ :-) (η γραμμή 52 ήταν σε άλλη κλάση ;))

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...