0

hello i whave an function which it take the number after the (.) and after "Vl" in string so i want call this function on the code but they tell me this error

non-static method Ajuster(String) cannot be referenced from a static context

this the code

public class Test {
     public Integer Ajuster(String data) { 
        Integer vlan=0;

        if (data.indexOf("Vl") >= 0) {
            int pos = data.indexOf("Vl") + 2;
            String vl = data.substring(pos, data.length());
            vlan=Integer.parseInt(vl.trim());           
        } else {
            int pos = data.lastIndexOf(".") + 1;
            String vl = data.substring(pos, data.length());
            try {
               vlan=Integer.parseInt(vl.trim()); 

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return vlan;
    }

    public static void main(String[] args) {

       Connection conn = null;
       try { 
          conn = DriverManager.getConnection("jdbc:mysql://localhost/mohammedia", "root", "123456"); 
          String sql = "SELECT * FROM router;"; 
          Telnet_Interface telnet = new Telnet_Interface();
          Telnet_Ressources telnet_R = new Telnet_Ressources();
          Telnet_Interface telnet1 = new Telnet_Interface();
          Telnet_Interface telnet2 = new Telnet_Interface();
          PreparedStatement prest = conn.prepareStatement(sql);
          ResultSet res=prest.executeQuery();
          while(res.next()){
             telnet1.Config(res.getString(1), "noc", "nocwana", res.getString(1));
             telnet2.Config(res.getString(2), "noc", "nocwana", res.getString(2));
          }
          ArrayList myData=telnet.getMyData();
          ArrayList myData1=telnet1.getMyData();
          ArrayList myData2=telnet2.getMyData();

          for(int i=0;i<myData1.size();i++)    
          {
             String data1=myData1.get(i).toString();
             Integer vl1=Ajuster(data1);
             System.out.print(vl1);
          }
       } 

}

so the problem it's about the line: Integer vl1=Ajuster(data1); Thank you

Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
cisco.nat
  • 55
  • 4

5 Answers5

0

You cant call a non-staic without any object reference.
Either make the method static like (depends if it dont involve any instance varialbe)

 public static Integer Ajuster(String data)  

or invoke with a object of class Test like

Test obj = new Test();
obj.Ajuster("data");

or better this http://docs.oracle.com/javase/tutorial/

PS: Method starting with capital name looks really weird

xyz
  • 21,367
  • 33
  • 107
  • 150
0

main is static. It means that it is not related to a instance of the Test class, but to the class itself.

Ajuster (please follow Java coding guidelines, it should be ajuster) is not static, so it is related to an instance of Test. So, to use it you must use it from a created instance (like this)

Test test = new Test();
test.ajuster();

or make it static (try not to overuse static methods)

SJuan76
  • 23,682
  • 6
  • 41
  • 79
0

It seems you're calling the method public Integer Ajuster(String data) that is non-static from the main that's, in fact, static. In order to call that method Ajuster you must instantiate an object of the class Test. I suppose you know how to do this, but however you must write this down Test test = new Test().

Federico
  • 501
  • 5
  • 13
0

You cannot call a non-static method with out creating an object. If it's a non-static context, however current object (this) will be there. If from static method you have to create an object and call the method on that object.

Static methods are all same for every object. In that case, we cannot know on what object are we applying the method or accessing a variable, that's why there is restriction.

Alternatively, you can make the method as static. But that depends. You should know when to use static methods and when not to use. It's a design issue.

Read:

To know the difference between static and non-static method

When should a method be static

How to call a non static method from main

So, create an object and call the method:

                   Test newTest = new Test();
                   newTest.ajuster();
Community
  • 1
  • 1
pinkpanther
  • 4,462
  • 2
  • 33
  • 57
0

You have to understand that non-static context cannot be referred in static context

public int test = 0;

public static void main(String[] args) {
  test += 4; //this wont compile
}

Non-static context however can include both non-static and static contexts. Try something like this.

public class Test {

public int test = 0;

  public static void main(String[] args) {
    new Test();
  }
  public Test() {
    test += 4; //this will compile
  }
}

If you cannot understand this, try to search and learn about Constructor

Arif Samin
  • 257
  • 1
  • 9