Search code examples
javavariablesarduino-yun

non-static variable cannot be referenced from a static context java


I am writing a program to connect to the console pixel example sketch with java. Im still prety new and I got this error:

non-static variable fast cannot be referenced from a static context

I don't know what the error means but my code is:

package javaapplication5;

import java.net.*;
import java.io.*;
import java.util.Scanner;
/**
 *
 * @author preferreduser
 */
public class JavaApplication5 {
    int fast = 0;
    public static void main(String[] args) throws IOException {
        Scanner x = new Scanner(System.in);
        System.out.print("Yun ip: ");
        String IP = x.nextLine();
        System.out.println("Loding...");
        try {
            // Create a URL for the desired page
            URL url = new URL("http://"+ IP +"/arduino/digital/13/1");       

            // Read all the text returned by the server
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        try {
            // Create a URL for the desired page
            URL url = new URL("http://"+ IP +"/arduino/digital/13/0");       

            // Read all the text returned by the server
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        System.out.println("Connected to YUN on "+ IP);
        OUTER:
            while (true) {
                Scanner y = new Scanner(System.in);
                System.out.print("> ");
                String str = y.nextLine();
                switch (str) {
                case "on":
                    try {
                        // Create a URL for the desired page
                        URL url = new URL("http://"+ IP +"/arduino/digital/13/1");

                        // Read all the text returned by the server
                        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                        in.close();
                    } catch (MalformedURLException e) {
                    } catch (IOException e) {
                    }               break;
                case "off":
                    try {
                        // Create a URL for the desired page
                        URL url = new URL("http://"+ IP +"/arduino/digital/13/0");

                        // Read all the text returned by the server
                        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                        in.close();
                    } catch (MalformedURLException e) {
                    } catch (IOException e) {
                    }               break;
                case "help":
                    System.out.println("");
                    System.out.println("on   exit");
                    System.out.println("off  help");
                    System.out.println("");
                    break;
                case "exit":
                    try {
                        // Create a URL for the desired page
                        URL url = new URL("http://"+ IP +"/arduino/digital/13/0");

                        // Read all the text returned by the server
                        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                        in.close();
                    } catch (MalformedURLException e) {
                    } catch (IOException e) {
                    }               break OUTER;
                }
                if ( fast == 1 ){
                    URL oracle = new URL("http://"+ IP +"/arduino/digital/13");
                    try (BufferedReader in = new BufferedReader(
                            new InputStreamReader(oracle.openStream()))) {
                        String inputLine;
                        while ((inputLine = in.readLine()) != null)
                            System.out.println(inputLine);
                    }
                } else {System.out.println("Success");}
            }
    }
}

I want to connect to an arduino yun and type commands like on or off and that part worked. I wanted to add an optional option fast to eliminate connecting to http:// * /aruino/digital/13 each time you typed in a command to make things faster. This was my start. I'm going to add a command for it but I can't until I get this figured out


Solution

  • change int fast = 0; to static int fast = 0;

    You are using variable fast in main method which is a static method. All the variables that are used in any static method should be static. The reason is static method is common for a class, it does not depend on the instance of the class. So it can not use any instance variable(unless you specify which particular instance to use) inside it because the method does not know which instance variable to use.