Problem with java compile error

Discussion in 'Software' started by BrokenArrows, Oct 19, 2005.

  1. BrokenArrows

    BrokenArrows Sergeant

    Im getting the following error and dont know why.

    Any help would be appreciated.
    Robot.java:30: non-static variable this cannot be referenced from a static context
    LightDetect LD = new LightDetect();


    Code:
    import josx.platform.rcx.*;
    import java.lang.*;
    import josx.robotics .*;
     
    
    
    public class Robot
    
    {
      int i=100;
      int id = 0;
      int light = 0;
      int total = 0;
      int cnt = 0;
      int average = 0;
    
    
      
      public static void main (String[] args)
      throws Exception
      {  
    	  
       
    	Motor.A.setPower(3);
    	Motor.C.setPower(3);
    	Motor.A.forward();
    	Motor.C.forward();
    
      LightDetect LD = new LightDetect();
      LD.start(); 
      }
    
    		public static void Stop()
     throws Exception
     {
      Motor.A.stop();
      Motor.C.stop();
     }
    
     public static void Turn(int degrees)
     throws Exception
     {
      Stop();
      
      Sensor.S3.setTypeAndMode(4,0xE0);
    	 Sensor.S3.activate();
    	 int tmp = Sensor.S3.readSensorValue(2,1);
       
    	 Motor.A.setPower(5);
    	 Motor.C.setPower(5);
    
    	 Motor.C.forward();
    	 Motor.A.backward();
    
    	 while (Sensor.S3.readSensorValue(2,1)-tmp<degrees/5.4) {
    	   LCD.showNumber(Sensor.S3.readSensorValue(2,1));
    	 }
    
    	 Stop();
    	 Stop();
      Motor.A.setPower(3);
    	 Motor.C.setPower(3);
      
      
     }
    
     public class LightDetect extends Thread
     {
    	 public void run(){  
    	  Sensor.S1.setTypeAndMode(SensorConstants.SENSOR_TYPE_LIGHT,
    		   SensorConstants.SENSOR_MODE_PCT); 
    	 Sensor.S1.activate();
    	 id = Sensor.S1.getId();
     
      while (i>0) {
    	   total = 0;
    	   for(cnt=0;cnt<10;cnt++){
    		light = Sensor.S1.readSensorValue(id,0);
    		   total = total + light;
    	   }
    		  average = total / 10;
    		  LCD.showNumber(average);
    	 
    		  if(average > 760 && average < 790){
    			 Stop();
    	   Turn(180);   
       }
    		
    		  if(average > 660 && average  < 690){
    	   Stop();
    			  Motor.A.forward();
    		  Motor.C.forward();
       }	
      }// End while
    	 
    			}
    	 } //end LightDetect();
       
    
    }
    
     
    
     
    
       
       
     
  2. Wookie

    Wookie Sergeant Major

    Is there some code missing, I see a instance called Motor and Sensor but I dont see the instance of the class being created anywhere.

    Are you creating and instance of sensor somewhere? such as MySensor Sensor = New Sensor();

    ?



    Sensor.S1.setTypeAndMode(SensorConstants.SENSOR_TYPE_LIGHT,
    SensorConstants.SENSOR_MODE_PCT);
    Sensor.S1.activate();
    id = Sensor.S1.getId();



    Also Heres a little snip on the error I found

    "Non-static Variable Cannot Be Referenced from a Static Context

    Lots of things can give you a non-static variable cannot be referenced from a static context error message. But for beginning programmers, the most common cause is having a variable that's declared outside of the main method. It's no sin to declare such a variable, but because the main method is always static, you need some special help to make the main method refer to a variable that's declared outside the main method.

    The quickest solution is to put the word static in front of the variable's declaration. But first, ask yourself why this variable's declaration isn't inside the main method. If there's no good reason, then move the variable's declaration so that it's inside the main method."

    Also check this thread

    http://forum.java.sun.com/thread.jspa?threadID=664628&messageID=3892406
     
  3. BrokenArrows

    BrokenArrows Sergeant

    thanks, i got it working in the end.

    No all the code is there. Those methods are being called from the imports.
     
  4. Wookie

    Wookie Sergeant Major

    So what did you change then? Someone may google this same problem in the future and be linked to here.
     
  5. BrokenArrows

    BrokenArrows Sergeant

    i actually just completely rewrote the code as i thought of a few more things that i needed to do and it would be very hard to implement in the current way i had it programmed.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds