0

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

almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
MrSchmuck
  • 55
  • 10
  • 2
    [Here](http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context), [here](http://stackoverflow.com/questions/15331846/non-static-variable-this-cannot-be-referenced-from-a-static-context) and [here](http://stackoverflow.com/questions/15962339/non-static-variable-this-cannot-be-referenced-from-a-static-context) amongst a few. Please google your issue before posting a question. – Mena Jul 06 '15 at 12:39
  • First things first, try and clear up your code a little; apart from formatting problems, there are pieces of code you might want to split into several methods – fge Jul 06 '15 at 12:39
  • Don't leave empty catch sections. At least print stacktrace. – Pshemo Jul 06 '15 at 12:40
  • @Pshemo the statement should be "do not leave empty catch sections, at least comment thier existence/purpose". Demanding at least a stacktrace would negate perfectly acceptable uses of exceptions such as NoSuchElement to be used to terminate a transformation loop. – Tuxxy_Thang Jul 06 '15 at 12:51
  • Just to add to other some answers: Problem is that `static` method doesn't use any instance to run (we are running it from class via `YourClass.staticMethod()`). In other words *there is no valid `this` in static method* (in its context) so code like `this.fast` (and that is what you are trying to do since `fast` is non-static field, which means it requires some instance, and if you don't specify any `this` is automatically added) doesn't make sense as there is no `this`. – Pshemo Jul 06 '15 at 13:00
  • @Tuxxy_Thang Yes you are right, but for novice programmer I would recommend printing/logging any error messages (it helps while learning and debugging). – Pshemo Jul 06 '15 at 13:02

2 Answers2

2

You can access member variable of a class directly only by making it static. Om making an variable static one more method to access it is by class_name.variable_name.

Otherwise you have to make an object of the class and then you can access that varible through that object.

Example:

  • either you change

    int fast=0; to static int fast = 0;

  • or you do this

    JavaApplication5 j5 = new JavaApplication5(); and now access the variable by j5.fast and do further calculations.

CoderNeji
  • 2,056
  • 3
  • 20
  • 31
1

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.

Karthik
  • 4,950
  • 6
  • 35
  • 65
  • 1
    While this does solve the problem in this case... This doesn't even try to explain what that error means or how it can be prevented in the future... Please try to help the OP learn why he faced the problem too... – Codebender Jul 06 '15 at 12:40
  • Sorry, I will edit :) – Karthik Jul 06 '15 at 12:41
  • "it can not use any instance variable" you need to be more specific to explain that by "use" you mean use without having to specify specific instance (since static methods can use non-static fields, as long as they have explicit instance like `instance.nonStaticField`). – Pshemo Jul 06 '15 at 12:51
  • @Pshemo thank you for making that point. Will keep it in mind :) – Karthik Jul 06 '15 at 13:02