Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: TimeUnit.SECONDS.sleep(x) stops simulation

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default TimeUnit.SECONDS.sleep(x) stops simulation

    Hey guys! I get a NullPointerException Error and really dont know what to do anymore ... Ive tried anything I know and nothing helped so I hope someone here can help me!

    I simulate bacteria and penezilin(shroom) in a petri dish. So heres the code

    bacteria:

    import java.util.ArrayList;
     
    public class Bakterium extends Lebewesen
    {
        //Vermehrungsrate 0.25; Sterberate 0.0
        public Bakterium(int position, int alter, ArrayList<Lebewesen> lebewesen)
        {
            super(position, alter, lebewesen, 0.25, 0.0);
        }
     
        public void vermehren()
        {
            Bakterium b;
            int wo;
            wo=super.platzSuchen();
            if (wo>0) {
                b = new Bakterium(wo, 0, super.lebewesenGeben());
     
                super.lebewesenGeben().add(b);
            }
     
            }
     
        public boolean istPilz()
        {
            return false;
        }
        }

    JFrame:

    import java.util.ArrayList;
    import javax.swing.*;
    import java.awt.*;
     
    public class Darstellung extends JFrame
    {
        ArrayList<Lebewesen> lebewesen;
     
        public Darstellung(ArrayList<Lebewesen> nLebenwesen)
        {
            super("Population"); //Name des Fensters/JFrames
            setSize(550,580);
            setVisible(true);
            lebewesen=nLebenwesen;
        }
     
           public void paint(Graphics g)
        {
            g.setColor(Color.yellow);
            g.fillRect(0, 0, 550, 580);
     
            g.setColor(Color.white);
            g.fillRect(25, 50, 500, 500);      
     
            Lebewesen l;
     
            for (int i=0; i<lebewesen.size(); i++)
            {
                l = lebewesen.get(i);
                if (l.istPilz()==false)
                {
                    g.setColor(Color.red);    
                }
                else
                {
                    g.setColor(Color.blue);
                }
     
                int x=(l.positionGeben()%100);
                if (x==0) {x=100;}
                int y=(l.positionGeben()-x);
                x=x*5+20;
                y=y/100*5+50;
                g.fillOval(x, y, 5, 5);
            }
        }
    }

    shroom:

    {
        public Pilz(int position, int alter, ArrayList<Lebewesen> lebewesen)
        {
            super(position, alter, lebewesen, 0.9, 0.2);
        }
     
        public void vermehren()
        {
            Pilz p;
            int wo;
            wo=super.platzSuchen();
            if (wo>0) {
                p=new Pilz(wo, 0, super.lebewesenGeben());
                super.lebewesenGeben().add(p);
            }
        }
     
        public boolean istPilz()
        {
            return true;
        }
     
        public boolean istNachbar(int pos)
        {
            int position=super.positionGeben();
            if ((position==(pos+1))||
                (position==(pos-1))||
                (position==(pos+99))||
                (position==(pos+100))||
                (position==(pos+101))||
                (position==(pos-99))||
                (position==(pos-100))||
                (position==(pos-101)))
                return true; else return false;
        }
     
        public void bakterienAbtoeten()
        {
            Lebewesen l=null;
            ArrayList<Lebewesen> lebewesen;
            lebewesen=super.lebewesenGeben(); 
            for (int i=0; i<lebewesen.size(); i=i++)
            {       
                    l=lebewesen.get(i);
                    if((istNachbar(l.positionGeben())==true)&& (l.istPilz()==false))
                    {lebewesen.remove(i);}
            } 
            lebewesen.trimToSize();        
        }   
        }

    Simlation:

    import java.util.ArrayList;
    import java.util.Random;
    import java.awt.*;
    import javax.swing.*;
     
    public class Simulation
    {
        private ArrayList<Lebewesen> lebewesen;
        private Random rand;
     
        public Simulation()
        {
            rand=new Random();
            lebewesen=new ArrayList<Lebewesen>();
        }
     
        public void lebewesenErzeugen(int anzBakterien, int anzPilze)
        {
            //random bakterium
            for (int i=0; i<anzBakterien; i=i+1)
            {
                Bakterium b;
                b=new Bakterium(rand.nextInt(10000)+1, rand.nextInt(5), lebewesen);
                lebewesen.add(b);
     
            }
            //random pilz
            for(int i=0; i<anzPilze; i=i+1)
            {
                Pilz p;
                p=new Pilz(rand.nextInt(10000)+1, 0, lebewesen);
                lebewesen.add(p);
            }
     
        }
     
        public void simulationStarten(int dauer)
        {
            Darstellung d=new Darstellung(lebewesen);
            lebewesenErzeugen(1,1);
            for (int i=0; i<dauer; i=i+1)
            {  
                pilzeToetenBakterienAb();
                alleLebewesenVermehrenSich();
                alleLebewesenAltern();
                lebewesen.trimToSize();
            }
        }
     
        private void alleLebewesenAltern()
        {
            Lebewesen l;
            double wahrscheinlichkeit;
            for (int i=0; i<lebewesen.size(); i++)
            {
                l=lebewesen.get(i);
                l.altern();
                wahrscheinlichkeit=rand.nextDouble();
                if ((wahrscheinlichkeit<l.pSterbenGeben()))
                {
                    lebewesen.remove(i);
                }
            }
        }
     
        private void pilzeToetenBakterienAb()
        {
            Lebewesen l;
            for (int i=0; i<lebewesen.size(); i=i+1)
            {
                l=lebewesen.get(i);
                l.bakterienAbtoeten();
            }
        }
     
        private void alleLebewesenVermehrenSich()
        {
            Lebewesen l;
            double wahrscheinlichkeit;
            int anzahl=lebewesen.size();
            for (int i=0; i<anzahl; i++)
            {
                l=lebewesen.get(i);
                wahrscheinlichkeit=rand.nextDouble();
                if ((wahrscheinlichkeit<l.pVermehrungGeben()))
                {
                    l.vermehren();
                }
            }
        }
     
        public int anzahlLebewesen()
        {
            int l;
            l=lebewesen.size();
            return l;
        }
    }

    lifeform:

    import java.util.ArrayList;
     
    public class Lebewesen{
     
        private int alter;
        private int position;
        private double pVermehrung;
        private double pSterben;
        private ArrayList<Lebewesen> lebewesen;
     
        public Lebewesen(int nPosition, int nAlter, ArrayList<Lebewesen> nLebewesen, 
        double nPVermehrung, double nPSterben)
        {
            position=nPosition;
            alter=nAlter;
            pVermehrung=nPVermehrung;
            pSterben=nPSterben;
        }
     
        public void altern()
        {
            alter=alter++;
            pSterben=pSterben+0.05;
        }
     
        private boolean istFrei(int wo)
        {
            boolean ergebnis;
            ergebnis=true;
            Lebewesen l;
            for (int i=0; i<lebewesen.size(); i++)
            {
                l=lebewesen.get(i);
                if (wo==l.positionGeben()) {ergebnis=false;}
            }
            return ergebnis;
        }
     
        public void vermehren()
        {
            //nur Pilze und Bakterien
        }
     
        public int platzSuchen()
        {
            int ergebnis=-1;
            //links
            if (istFrei(position-1)) ergebnis=position-1; else
            // links oben
            if (istFrei(position-101)) ergebnis=position-101; else 
            // oben
            if (istFrei(position-100)) ergebnis=position-100; else
            // rechts oben
            if (istFrei(position-99)) ergebnis=position-99; else
            // rechts
            if (istFrei(position+1)) ergebnis=position+1; else
            // rechts unten
            if (istFrei(position+101)) ergebnis=position+101; else
            // unten
            if (istFrei(position+100)) ergebnis=position+100; else
            // links unten
            if (istFrei(position+99)) ergebnis=position+99; else
            // nichts frei
            if (ergebnis<1 || ergebnis>10000) ergebnis=-1;
            return ergebnis;
        }
     
        // weitere Methoden
        public int alterGeben() {return alter;}
        public int positionGeben() {return position;}
        public double pSterbenGeben() {return pSterben;}
        public double pVermehrungGeben() {return pVermehrung;}
        public ArrayList<Lebewesen> lebewesenGeben(){return lebewesen;}
     
        //wird benötigt auch wenn später überschrieben
        public boolean istPilz() {return true;}
        public void bakterienAbtoeten() { /**void -> kein return**/}
    }

    Yes I am from Germany.

    So when I create an object of simulation and call the method simulationStarten(int dauer) [dauer means duration] it opens the JFrame with 2 dots, the created shroom and bacteria and then it says:

    java.lang.NullPointerException
    at Pilz.bakterienAbtoeten(Pilz.java:45)
    at Simulation.pilzeToetenBakterienAb(Simulation.java: 72)
    at Simulation.simulationStarten(Simulation.java:43)

    the method with the problem is:

       public void bakterienAbtoeten()
        {
            Lebewesen l;
            ArrayList<Lebewesen> lebewesen;
            lebewesen=super.lebewesenGeben(); 
            [I]for (int i=0; i<lebewesen.size(); i=i++)[/I]
            {       
                    l=lebewesen.get(i);
                    if((istNachbar(l.positionGeben())==true)&& (l.istPilz()==false))
                    {lebewesen.remove(i);}
            } 
            lebewesen.trimToSize();        
        }

    the fat code is marked. And if I delete this method out of simulationStarten(int dauer) the same error occours in:

        private boolean istFrei(int wo)
        {
            boolean ergebnis;
            ergebnis=true;
            Lebewesen l;
            [B]for (int i=0; i<lebewesen.size(); i++)[/B]
            {
                l=lebewesen.get(i);
                if (wo==l.positionGeben()) {ergebnis=false;}
            }
            return ergebnis;
        }

    what is the problem with my "for loop"?

    I hope someone can help me!

    Thanks in advance!


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: NullPointerException in a "for loop"

    In the Lebewesen class you have this private member:

    private ArrayList<Lebewesen> lebewesen;

    and this constructor:

    public Lebewesen(int nPosition, int nAlter, ArrayList<Lebewesen> nLebewesen, 
        double nPVermehrung, double nPSterben)

    The private member is never set. You are missing a single line of code:

    lebewesen = nLebwesen;

  3. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    CrusoCraruso (January 20th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: NullPointerException in a "for loop"

    This is the kind of thought process you should follow when debugging your code.

    Let's focus on the problem as indicated by the error:
    ArrayList<Lebewesen> lebewesen;
    lebewesen=super.lebewesenGeben(); 
     
    // the error statement, indicating that lebewesen is null
    for (int i=0; i<lebewesen.size(); i=i++)

    I've indicated by the comment above what I think is happening, and the two statements previous suggest:

    1. The ArrayList lebewesen is created locally, not initialized
    2. The statement:

    lebewesen=super.lebewesenGeben();

    does not change #1.

    So, searching for the method , I found this in class Lebewesen:
    public ArrayList<Lebewesen> lebewesenGeben()
    {
        return lebewesen;
    }

    Then, looking for the initialization of lebewesen, something like:

    lebewesen = new ArrayList<Lebewesen>();

    I couldn't find anything similar in class Lebewesen, though I see it in class Simulation. I admit that I get a bit distracted by the German variables, but I don't think that's the problem. Where is lebewesen initialized in class Lebewesen so that lebewesenGeben() returns something other than null?
    Last edited by GregBrannon; January 20th, 2014 at 10:22 AM. Reason: Forgot to add code tags.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    CrusoCraruso (January 20th, 2014)

  6. #4
    Junior Member
    Join Date
    Jan 2014
    Location
    Saint-Petersburg, Russia
    Posts
    8
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: NullPointerException in a "for loop"

    At first glance private ArrayList<Lebewesen> lebewesen of class Lebewesen is not being initialized. That's why you getting NPE.

  7. The Following User Says Thank You to trod For This Useful Post:

    CrusoCraruso (January 20th, 2014)

  8. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException in a "for loop"

    I really forgot that single line of code "lebewesen = nLebwesen;" in the constructor of Lebewesen. Cant believe I forgot that.

    Thanks to all of you. That solved it actually!!

    --- Update ---

    Hey guys! I got a new problem ^^

    Why does when I call "warte(int)" the simulation stop?

    import java.util.ArrayList;
    import java.util.Random;
    import java.awt.*;
    import javax.swing.*;
    import java.util.concurrent.TimeUnit;
     
    public class Simulation
    {
        private ArrayList<Lebewesen> lebewesen = new ArrayList<Lebewesen>();
        private Random rand;
     
        public Simulation()
        {
            rand=new Random();
            lebewesen=new ArrayList<Lebewesen>();
        }
     
        public void lebewesenErzeugen(int anzBakterien, int anzPilze)
        {
            //random bakterium
            for (int i=0; i<anzBakterien; i=i+1)
            {
                Bakterium b;
                b=new Bakterium(rand.nextInt(10000)+1, rand.nextInt(5), lebewesen);
                lebewesen.add(b);
     
            }
            //random pilz
            for(int i=0; i<anzPilze; i=i+1)
            {
                Pilz p;
                p=new Pilz(rand.nextInt(10000)+1, 0, lebewesen);
                lebewesen.add(p);
            }
     
        }
     
        public void simulationStarten()
        {
            int dauer=999999999;
            Darstellung d=new Darstellung(lebewesen);
            lebewesenErzeugen(1,1);
            for (int i=0; i<dauer; i++)
            {  
                pilzeToetenBakterienAb();
                alleLebewesenVermehrenSich();
                alleLebewesenAltern();
                lebewesen.trimToSize();
                warte(1);
            }
        }
     
        private void alleLebewesenAltern()
        {
            Lebewesen l;
            double wahrscheinlichkeit;
            for (int i=0; i<lebewesen.size(); i++)
            {
                l=lebewesen.get(i);
                l.altern();
                wahrscheinlichkeit=rand.nextDouble();
                if ((wahrscheinlichkeit<l.pSterbenGeben()))
                {
                    lebewesen.remove(i);
                }
            }
        }
     
        private void pilzeToetenBakterienAb()
        {
            Lebewesen l;
            for (int i=0; i<lebewesen.size(); i=i+1)
            {
                l=lebewesen.get(i);
                l.bakterienAbtoeten();
            }
        }
     
        private void alleLebewesenVermehrenSich()
        {
            Lebewesen l;
            double wahrscheinlichkeit;
            int anzahl=lebewesen.size();
            for (int i=0; i<anzahl; i++)
            {
                l=lebewesen.get(i);
                wahrscheinlichkeit=rand.nextDouble();
                if ((wahrscheinlichkeit<l.pVermehrungGeben()))
                {
                    l.vermehren();
                }
            }
        }
     
        private int anzahlLebewesen()
        {
            int l;
            l=lebewesen.size();
            return l;
        }
     
        private void warte(int ms)
        {
            try
            {
                TimeUnit.SECONDS.sleep(ms);
            }
            catch (Exception e)
            {
                //nichts tun
            }
        }
    }

  9. #6
    Junior Member
    Join Date
    Jan 2014
    Location
    Saint-Petersburg, Russia
    Posts
    8
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: TimeUnit.SECONDS.sleep(x) stops simulation

    Looks like you set default thread to sleep. So it's normal. How do you call the warte(int ms) ?

  10. #7
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: TimeUnit.SECONDS.sleep(x) stops simulation

    Quote Originally Posted by trod View Post
    Looks like you set default thread to sleep. So it's normal. How do you call the warte(int ms) ?
    I call it with the method simulationStarten()

        public void simulationStarten()
        {
            int dauer=999999999;
            Darstellung d=new Darstellung(lebewesen);
            lebewesenErzeugen(1,1);
            for (int i=0; i<dauer; i++)
            {  
                pilzeToetenBakterienAb();
                alleLebewesenVermehrenSich();
                alleLebewesenAltern();
                lebewesen.trimToSize();
                warte(1);
            }
        }

    Therer I insert "1" so it waits 1s per loop. But somehow the loop just stops without any error when it hits this line.

  11. #8
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: TimeUnit.SECONDS.sleep(x) stops simulation

    But dauer (duration?) is 999999999.

    Then you are running a for loop from 0 to 999999999 and it hangs for 1 second each iteration. I am guessing that 31.6888 years to complete the loop isn't the desired behavior.
    Last edited by ChristopherLowe; January 20th, 2014 at 12:45 PM. Reason: type-o

  12. #9
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: TimeUnit.SECONDS.sleep(x) stops simulation

    Yea I just wanted that the simulation gets to its end. Means that every bacteria is being removed by the penecilin. But that doesnt occur the "error". When I write the
    method simulationStarten() like this:

    public void simulationStarten(int dauer)
        {
            Darstellung d=new Darstellung(lebewesen);
            lebewesenErzeugen(1,1);
            for (int i=0; i<dauer; i++)
            {  
                pilzeToetenBakterienAb();
                alleLebewesenVermehrenSich();
                alleLebewesenAltern();
                lebewesen.trimToSize();
                warte(1);
            }
        }

    the loop still ends without any error after it hits the method warte(int ms)

Similar Threads

  1. while loop just stops looping
    By dstars5 in forum Loops & Control Statements
    Replies: 23
    Last Post: March 3rd, 2014, 01:17 AM
  2. Time String to int seconds
    By willemuk in forum Java Theory & Questions
    Replies: 9
    Last Post: March 7th, 2012, 12:14 PM
  3. Setting Boolean to 'True' after X amount of Seconds?
    By uhKenKaniff in forum Java Theory & Questions
    Replies: 1
    Last Post: December 12th, 2011, 07:29 PM
  4. I want to extract minutes and seconds from duration
    By msa969 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 17th, 2011, 02:14 PM
  5. While loop stops.
    By MrFish in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 16th, 2010, 06:40 PM