Results 1 to 11 of 11
  1. #1
    senca is offline Member
    Join Date
    Feb 2010
    Posts
    36
    Rep Power
    0

    Default 3 Nullpointerexeptions I can't get rid off

    hey,
    I've written 3 classes: Wagon, Locomotief and a class Trein.
    In the Trein class I have 3 nullpointerexeptions I can't get rid off. They occur in the Trein class at the last 3 methods I've written. I must be overlooking something small and stupid :).

    Wagon:

    Java Code:
    public class Wagon{
        private int maxaantpas=80;
        private double maxaantgoed=1500.0;
        private int aantalPas;
        private double gewichtGoederen;
        
    
        
    public int getMaxaantpas(){
        return this.maxaantpas;}
    public double getMaxaantgoed(){
        return this.maxaantgoed;}
    public int getAantalPas(){
        return this.aantalPas;}
    public double getGewichtGoederen(){
        return this.gewichtGoederen;}
        
    private void setMaxaantpas(int maxaantpas){
        this.maxaantpas=maxaantpas;}
    private void setMaxaantgoed(double maxaantgoed){
        this.maxaantgoed=maxaantgoed;}
    private void setAantalpas(int aantalPas){
        this.aantalPas=aantalPas;}
    private void setGewichtGoederen(double aantalGoederen){
        this.gewichtGoederen=gewichtGoederen;}
        
    public boolean stapOp(){
        boolean gelukt=false;
        if(aantalPas<80)
        aantalPas++;
        gelukt=true;
        return gelukt;}
        
    public boolean stapUit(){
        boolean gelukt=false;
        if(aantalPas>0)
            aantalPas--;
            gelukt=true;
            return gelukt;}
            
    public boolean laad(double gewicht){
        boolean gelukt=false;
        if(gewicht>0.0 && gewicht <= 1500.0 && gewicht+gewichtGoederen<=1500.0)
            gelukt=true;
            gewichtGoederen+=gewicht;
    
            return gelukt;
      
        }
            
    public boolean los(double gewicht){
        boolean gelukt=false;
        if(gewicht>=1.0 && gewichtGoederen>1.0 && gewicht <= gewichtGoederen)
            gelukt=true;
            gewichtGoederen=gewichtGoederen-gewicht;
            return gelukt;}
    }
    Locomotief:

    Java Code:
    public class Locomotief{
        private String locCode = "Onbekend";
        private int maxSnelheid = 120;
        private int maxAantalPas = 40;
        private int aantalPas;
        
    public Locomotief(String locCode, int maxSnelheid, int maxAantalPas, int aantalPas){
        this.setLocCode(locCode);
        this.setMaxSnelheid(maxSnelheid);
        this.setMaxAantalPas(maxAantalPas);
        this.setAantalPas(aantalPas);}
        
    public String getLocCode(){
        return this.locCode;}
    public int getMaxSnelheid(){
        return this.maxSnelheid;}
    public int getMaxAantalPas(){
        return this.maxAantalPas;}
    public int getAantalPas(){
        return this.aantalPas;}
        
    private void setLocCode(String locCode){
        this.locCode=locCode;}
    private void setMaxSnelheid(int maxSnelheid){
        this.maxSnelheid=maxSnelheid;}
    private void setMaxAantalPas(int maxAantalPas){
        this.maxAantalPas=maxAantalPas;}
    private void setAantalPas(int aantalPas){
        this.aantalPas=aantalPas;}
        
    public boolean stapOp(){
        boolean gelukt=false;
        if(aantalPas<40)
            aantalPas++;
            gelukt=true;
            return gelukt;}
            
    public boolean stapUit(){
        boolean gelukt=false;
        if(aantalPas>=1)
            aantalPas--;
            gelukt=true;
            return gelukt;}
            
    }
    Trein:

    Java Code:
    public class Trein{
        private Locomotief locomotief;
        private int aantalWagons;
        private Wagon[] wagons;
        
    public Trein(Locomotief locomotief, int aantalWagons){
        this.setLocomotief(locomotief);
        this.setAantalWagons(aantalWagons);}
        
    public Locomotief getLocomotief(){
        return this.locomotief;}
    public int getAantalWagons(){
        return this.aantalWagons;}
    public Wagon[] getWagons(){
        return this.wagons;}
        
    public void setLocomotief(Locomotief locomotief){
        if(locomotief != null)
            this.locomotief=locomotief;}
            
    public void setAantalWagons(int aantalWagons){
        if(aantalWagons>0){
                this.aantalWagons=aantalWagons;}}
                
    public void setWagons(Wagon[] wagons){
        if(wagons != null)
            this.wagons=wagons;}
            
    public boolean addWagon(Wagon wagon){
        boolean stink = false;
        
               if(wagon != null)
                wagons[getAantalWagons()] = wagon;
               stink=true;
        return stink;}
        
    public boolean stapOpWagon(int i){
    
        boolean opgestapt=false; 
        if(i>0 && i<=getAantalWagons() && wagons[i].getAantalPas()<wagons[i].getMaxaantpas())
            wagons[i].stapOp();
        opgestapt = true;
        return opgestapt;}
        
    public boolean laad(double gewicht){
        boolean gelukt=false;
        for(int i=0; i<=getAantalWagons(); i++)
        if(gewicht <= 1500.0 && gewicht > 0 && wagons[i].getGewichtGoederen() + gewicht <= 1500.0)
            wagons[i].laad(gewicht);
        return gelukt;}
           
           
    
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    senca is offline Member
    Join Date
    Feb 2010
    Posts
    36
    Rep Power
    0

    Default

    addWagon:

    java.lang.NullPointerException
    at Trein.addWagon(Trein.java:33)

    laad:

    java.lang.NullPointerException
    at Trein.laad(Trein.java:48)

    stapOp:

    java.lang.NullPointerException
    at Trein.stapOpWagon(Trein.java:40)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,360
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by senca View Post
    addWagon:

    java.lang.NullPointerException
    at Trein.addWagon(Trein.java:33)

    laad:

    java.lang.NullPointerException
    at Trein.laad(Trein.java:48)

    stapOp:

    java.lang.NullPointerException
    at Trein.stapOpWagon(Trein.java:40)
    You never initialize your 'wagons' array in your 'Trein' class so it's null.

    kind regards,

    Jos

  5. #5
    senca is offline Member
    Join Date
    Feb 2010
    Posts
    36
    Rep Power
    0

    Default

    I'm getting arrayoutofboundsexeption on every method in the trein class :s lik eaddWagon, stapOp, ... and I can't figure out why :s

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,360
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by senca View Post
    I'm getting arrayoutofboundsexeption on every method in the trein class :s lik eaddWagon, stapOp, ... and I can't figure out why :s
    The exception also lists the actual index value that cause the OOBE, that should give you a clue ...

    kind regards,

    Jos

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yeah, if you read a bit your error message then you can find the reason cause this. Check on initializations ans the indexes accessing within your code in each array.

  8. #8
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    add a null check: (like you did)
    if (object != null) {
    /continue processing...
    }
    else {
    System.out.println("Object is null!");
    }
    This is how i usually fix bugs; By printing what happens, And i see what could be the possible cause.

  9. #9
    senca is offline Member
    Join Date
    Feb 2010
    Posts
    36
    Rep Power
    0

    Default

    I just don't see it. I've initialized my variables like this:

    public class Trein{
    private Locomotief locomotief;
    private int aantalWagons=0;
    private Wagon[] wagons = new Wagon[aantalWagons];

    This way I'm starting off with a "locomotief" and 0 wagons. But it keeps gving me exeptions :s.

    I've tried to change the method to like this:

    public boolean addWagon(Wagon wagon){
    boolean stink = false;

    if(wagon != null)
    wagons[aantalWagons] = wagon;
    aantalWagons++;
    stink=true;
    return stink;}

    I tried "wagons[getAantalWagons()]" ; "wagons[getAantalWagons()-1]" ; wagons[aantalWagons]" and wagons[aantalWagons-1]"...trying to refer to a position in the array to put in the new wagon but I just can't find the problem.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,360
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by senca View Post
    I just don't see it. I've initialized my variables like this:

    public class Trein{
    private Locomotief locomotief;
    private int aantalWagons=0;
    private Wagon[] wagons = new Wagon[aantalWagons];

    This way I'm starting off with a "locomotief" and 0 wagons. But it keeps gving me exeptions :s.

    I've tried to change the method to like this:

    public boolean addWagon(Wagon wagon){
    boolean stink = false;

    if(wagon != null)
    wagons[aantalWagons] = wagon;
    aantalWagons++;
    stink=true;
    return stink;}
    Nowhere do you change your array of zero elements to one with more elements. If I were you I'd use a List<Wagon> and be done with it.

    kind regards,

    Jos

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    If you must use an array to hold the data, create one that is large enough to hold all that you can possibly need to hold and have a separate int variable that counts the number of items in the array. Perhaps:
    private Wagon[] wagons = new Wagon[MaxPossibleNbrWagons];

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •