Heya, still rather new to programming, I'm having trouble understanding NullPointerException. The code below throws NullPointerException at rows 30 and 92. I do not quite understand why (as I understand it row 30 won't be run before the objects are created)? Any help would be appreciated!!

public class Uppg5_OH {
    
    static class Ryggsack {
    private Sak[] ryggsackArray = new Sak[30];
    private int arrayCounter = 0;
        public Ryggsack() {
            
        }
        public void stoppaIn(Sak s) {
            if (arrayCounter<29) {
                ryggsackArray[arrayCounter] = s;
                arrayCounter++;
                }
            else {
                System.out.println("Ryggsäcken är full!");
            }
        }
        public int totalVikt() {    
            int totalaVikten = 0;
            for (int i=0; i<ryggsackArray.length; i++) {
                totalaVikten += ryggsackArray[i].Vikt();
            }
            return totalaVikten;
        }
        public Sak tyngstaSaken() {
            Sak tyngsta = null;
            int störstaVikt = 0;
            for (int j=0; j<ryggsackArray.length; j++) {
                int vikten = ryggsackArray[j].Vikt();
                if ( j==0 ) {
                    ryggsackArray[j] = tyngsta;
                    störstaVikt = vikten;
                }
                else {
                    if (störstaVikt < vikten) {
                        ryggsackArray[j] = tyngsta;
                        //störstaVikt = vikten;
                    }
                }
            }
                    
            return tyngsta;
            
        }
        public int antalSmaSaker() {
            int mgdSmaSaker = 0;
            for (int k=0; k<ryggsackArray.length; k++) {
                if (ryggsackArray[k].Vikt()<2) {
                    mgdSmaSaker++;
                }
            }
            return mgdSmaSaker;
        }
    }
    static class Sak {
        private String namn;
        private int tyngd;
        
        public Sak(String beteckning, int vikt) {
            namn    = beteckning;
            tyngd   = vikt;
        }
        public java.lang.String getBeteckning()  {
            return this.namn; 
        }
        public int Vikt() {
            return this.tyngd;
        }
    }
    
    
    
 
        public static void main(String[] args) { 
            // En ny ryggsäck skapas 
            Ryggsack fjallraven = new Ryggsack(); 
            // Sätter in några saker i ryggsäcken  
            fjallraven.stoppaIn(new Sak("Bok",3)); 
            fjallraven.stoppaIn(new Sak("Tröja",1)); 
            fjallraven.stoppaIn(new Sak("Jacka",1)); 
            fjallraven.stoppaIn(new Sak("Stövlar",2)); 
            fjallraven.stoppaIn(new Sak("Sovsäck",4));
            System.out.println("Total vikt: " + fjallraven.totalVikt()); 
            System.out.println("Tyngsta saken: " + fjallraven.tyngstaSaken().getBeteckning()); 
            System.out.println("Antal småsaker: " + fjallraven.antalSmaSaker()); 
        }
    }

ryggsackArray[arrayCounter] = s;

I think that is where your problem lies.Try using a copy constructor

Also, if possible paste what output you are getting.Is it atleast printing Total Vikt ?

Exception in thread "main" java.lang.NullPointerException
at Uppg5_OH$Ryggsack.totalVikt(Uppg5_OH.java:30)
at Uppg5_OH.main(Uppg5_OH.java:92)
Java Result: 1

I will familiarize myself with copy constructor and let you know how it goes, thanks!

Sorry, this is nothing to do with copy constructors.
You create an array and give it a size (30), and initially all those elements are null. You have a method to populate the array with data, but unless you call that 30 times, there will still be null elements in the array.
Your loops then process every element in the array, even the null ones, and when you do that you get an NPE.
Rather than loop through all the array elements, use your arraycounter variable to limit the loop to just the elements that have been populated.

Edited 4 Years Ago by JamesCherrill: n/a

Sorry, this is nothing to do with copy constructors.
You create an array and give it a size (30), and initially all those elements are null. You have a method to populate the array with data, but unless you call that 30 times, there will still be null elements in the array.
Your loops then process every element in the array, even the null ones, and when you do that you get an NPE.
Rather than loop through all the array elements, use your arraycounter variable to limit the loop to just the elements that have been populated.

Very informative and helpful, thanks!

This question has already been answered. Start a new discussion instead.