Java va rog ajutor: NullPointerException

public void setPositions()
{
this.bottom.setXPos(100);
this.bottom.setYPos(200);
this.topLeft.setXPos(0);
this.topLeft.setYPos(100);
this.topRight.setXPos(200);
this.topRight.setYPos(100);
}
la metoda asta am ca rezultat “java.lang.NullPointerException” de ce e asa?
Multumesc.

pune codul complet

ai pe undeva null.

Vezi cu breakpoint-ul ce este null pe acolo

public class LevelCrossingController
{  
   private Light topLeft;
   private Light topRight;
   private Light bottom;
   private int state;
   private boolean trainComing;
   public final static int MIN_REPEATS = 4;
    /**
    * Constructor for objects of class LevelCrossingControler
    */
    public LevelCrossingController(Light topLeft ,Light topRight, Light bottom)
    {
      bottom=new Light();
      topLeft =new Light();
      topRight =new Light();
     
      int state=0;
      trainComing=false;
    }
    
    /**
     * Sets the positions of the lights.
     */
    public void setPositions() 
    {
       this.bottom.setXPos(100);
       this.bottom.setYPos(200);      
       this.topLeft.setXPos(0);
       this.topLeft.setYPos(100);      
       this.topRight.setXPos(200);
       this.topRight.setYPos(100);
    } 
    
    public void colourLight(Light colour ,OUColour  aColour )
    {
        this.topLeft.setColour(aColour );
        this.topRight.setColour(aColour);
        this.bottom.setColour(aColour);
    }
     /**
     * @return trainComing
     */
    public boolean getTrainComing()
    {
        return trainComing;
    }
    /**
     * 
     */
    public void changeState()
    { 
       if(trainComing = true ){
          if (state == 0){
         state=1;}else if(state==1){
         state=2;}else if(state==3){
         state=3;}else{state=0;}
      }}
      
    /**
     * 
     */  
    public void colourAllLights(){
      if (state ==0){
         this.bottom.setColour( OUColour.BLACK);
         this.topLeft.setColour( OUColour.BLACK);
         this.topRight.setColour( OUColour.BLACK);
         this.setTrainComing(false);} 
      if (state ==1){ 
         this.bottom.setColour( OUColour.ORANGE);
         this.topLeft.setColour( OUColour.BLACK);
         this.topRight.setColour( OUColour.BLACK);
         this.setTrainComing(true);} 
      if (state ==2){
         this.bottom.setColour( OUColour.BLACK);
         this.topLeft.setColour( OUColour.RED);
         this.topRight.setColour( OUColour.BLACK);
         this.setTrainComing(true);} 
      if (state ==3){
         this.bottom.setColour( OUColour.BLACK);
         this.topLeft.setColour( OUColour.BLACK);
         this.topRight.setColour( OUColour.RED);
         this.setTrainComing(true);} 
      
      }
      
    /**
     * @param trainComing
     * setter for trainComing
     */
    public void setTrainComing(boolean trainComing)
    {
        this.trainComing = trainComing;
    }
      
    
  
    /**
    * Find out how many times red lights should flash at the crossing.
    * Simulates length of train at crossing.
    */   
    public static int findNumRepeats()
    {
       int repeats = 0;
       String timesAsString =              
       OUDialog.request("How many times should the red lights"
         + " flash? ("
          + LevelCrossingController.MIN_REPEATS
          + " or over times)");                 
      if (timesAsString != null)
       {
          repeats = Integer.parseInt(timesAsString);
       }
       return repeats;
   }
    
   /**
    * Causes execution to pause for a number of milliseconds.
    */
    public static void delay(int time)
    {
       try
       {
          Thread.sleep(time); 
       }
       catch (Exception e)
       {
          System.out.println(e);
       } 
     }
     /**
      * 
      */
     public void doTrainApproaching(){
       System.out.println ("Train approaching");
       int x=findNumRepeats();
       setTrainComing(true);
       
       state=0;
       colourAllLights();
       delay(500);
        for(int i=0 ;i==x; i++){
         state=1;
         colourAllLights();
         delay(500);
         state=2;
         colourAllLights();
         delay(500);
         state=3;
         colourAllLights();
         delay(500);
       }
    }

Pune eroarea completa. De obicei iti indica exact linia si vei stii ce nu e initializat acolo

1 Like

ia scote this din metoda aia.
Daca este in clasa, nu cred ca mai este nevoie de el

si vad ca sunt initializate in constructor.

siiii
cred ca de aici vin nule

   private Light topLeft;
   private Light topRight;
   private Light bottom;

Mie nu-mi place ce faci pe-aici. Cred ca nu face ce crezi tu ca face.

Faza e că dacă folosea “this” în constructor cel mai probabil nu mai primea acele excepții legate de null :slight_smile:

Senzația mea e ca de fapt încerca să paseze acele “lights” ca parametri în constructor, după care s-a răzgândit și a încercat să le instanțieze “in-place”, doar că în loc să le asigneze membrilor obiectului, le-a asignat variabilelor locale care i-au venit ca argument.

@rad, cred că îți lipsesc cunoștințe de bază legate de programarea pe obiecte și de programarea în general. Ce anume încerci să faci?

Corect, acolo era buba: e un constructor care ar trebui sa initializeze atributele proprii pe baza parametrilor primiti.

public LevelCrossingController(Light topLeft ,Light topRight, Light bottom)
    {
      this.bottom = bottom;
      this.topLeft = topLeft;
      this.topRight = topRight;
     
      this.state=0;
      this.trainComing=false;
    }
1 Like

Am rezolvat ;multumesc la toti