Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Code problem

  • 19-10-2005 3:10pm
    #1
    Registered Users, Registered Users 2 Posts: 7,501 ✭✭✭


    Im getting this error and cannot see why.

    Can anyone help me. Thanks.

    Robot.java:30: non-static variable this cannot be referenced from a static context


    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();
    			
    
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 304 ✭✭PhantomBeaker



    Robot.java:30: non-static variable this cannot be referenced from a static context

    [code]
    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();
    }

    Ok, perversely line 30 is the LightDetect one - which doesn't appear to be a problem. At least the 30th line from when I paste it into a text editor.

    Anyway, where do you declare Motor.A and Motor.C in your class? They're not passed in anywhere, and don't seem to be declared in the class anywhere. That looks to be your main problem - so you need to declare your variables/objects somewhere and then start passing them to the other methods that need to see them (like your stop() method).

    Hope that helps a bit,
    Aoife


  • Registered Users, Registered Users 2 Posts: 2,243 ✭✭✭zoro


    It's actually pretty simple to fix :)

    I'm assuming that LightDetect extends Thread here by the way (it appears so anyway)
    When the compiler says line 30, you should check lines 29, 30 and 31 - it's not always on the button for some reason :/

    Anyway - add this:
    LightDetect LD = new LightDetect();
    new Thread(LD).start();
    

    You can't explicitly call LightDetect.start() as it's a non-static method (you're inside a static method when you call it;

    That should work fine.
    If it doesnt, try:
    public static void main (String[] args) throws Exception
    {
     new MyRobotClass();
    }
    
    public MyRobotClass() throws Exception
    {
      Motor.A.setPower(3);
      Motor.C.setPower(3);
      Motor.A.forward();
      Motor.C.forward();
    
      LightDetect LD = new LightDetect();
      LD.start();
    }
    


  • Closed Accounts Posts: 324 ✭✭madramor


    The first way zoro said will not work because its the same

    The second way will because it takes any call to a non-static
    variable or method out of the static method main.

    The simpliest way is to take the class LightDetect out of the
    class robot and put it in its own class file.


  • Registered Users, Registered Users 2 Posts: 7,501 ✭✭✭BrokenArrows


    thanks for the help. This will help me in the future anyway.

    I ended up rewriting the code as there are a few things i did not take into account.


  • Registered Users, Registered Users 2 Posts: 2,243 ✭✭✭zoro


    madramor wrote:
    The first way zoro said will not work because its the same
    *slaps forehead* doh!

    Oopies :)


  • Advertisement
  • Closed Accounts Posts: 324 ✭✭madramor


    I ended up rewriting the code as there are a few things i did not take into account.

    look at the code zoro posted, none of your classes have constructors

    also
    public static void main (String[] args) throws Exception
    {
     new MyRobotClass();
    }
    
    main shouldn't throw exceptions it should be
    public static void main (String[] args){
       try{
          new MyRobotClass();
       }
       catch(Exception e){
          // handle
       }
    }
    


Advertisement