Can't make static reference to method while it is static

Hello, can somebody please help me with my problem. I created a jsp page wich includes a .java file I wrote. In this JSP I called a method in the class I created. It worked but when I made the method static and adjusted the calling of the method it started to complain while i didnt make an instance of the class. the error is:Can't make static reference to method
here is the code for the class and jsp:
public class PhoneCheckHelper {
public static String checkPhoneNumber(String phoneNumber) {
String newPhoneNumber ="";
for(int i=0; i<phoneNumber.length(); i++) {
char ch = phoneNumber.charAt(i);
if(Character.isDigit(ch)) {
newPhoneNumber += String.valueOf(ch);
return newPhoneNumber;
<html>
<head>
<title>phonecheck_handler jsp pagina</title>
<%@page import="java.util.*,com.twofoldmedia.text.*, java.lang.*" %>
</head>
<body>
<input type="text" value="<%= PhoneCheckHelper.checkPhoneNumber(request.getParameter("phonenumberfield")) %>">
</body>
</html>

Go over to the "New to Java" forum where that message is frequently explained. Do a search if you don't see it in the first page of posts.

Similar Messages

  • Can't make static reference to method

    hi all,
    pls help me in the code i'm getting
    " can't make static reference to method....."
    kindly help me
    the following code gives the error:
    import java.rmi.*;
    import java.rmi.Naming;
    import java.rmi.RemoteException;
    import java.io.*;
    import java.io.IOException;
    import java.io.LineNumberReader;
    public class client
    static String vcno;
    static String vpin;
    static String vamt;
    static String vordid;
    static String vptype;
    //static String vreq;
    static String vreq = "1";
    static String vorgid;
    static String vm1;
    static String vs1;
    static String vqty1;
    static String vm2;
    static String vs2;
    static String vqty2;
    static String vm3;
    static String vs3;
    static String vqty3;
    static String vm4;
    static String vs4;
    static String vqty4;
    public static void main(String args[])
    try
    ServerIntf serverintf=(ServerIntf)Naming.lookup("rmi://shafeeq/server");
    int c;
    StringBuffer sb;
    FileReader f1=new FileReader("c:/testin.txt");
    sb=new StringBuffer();
    int line=0;
    while ((c=f1.read())!=-1) {
    sb.append((char)c);
    LineNumberReader inLines = new LineNumberReader(f1);
    String inputline;
    String test;
    while((inputline=inLines.readLine())!=null)
    line=inLines.getLineNumber();
    switch(line)
    case 1: {
    vcno = inLines.readLine();
    System.out.println(vcno);
    case 2: {
    vpin = inLines.readLine();
    System.out.println(vpin);
    case 3: {
    vptype = inLines.readLine();
    System.out.println(vptype);
    case 4: {
    vamt = inLines.readLine();
    System.out.println(vamt);
    case 5: {
    vordid = inLines.readLine();
    System.out.println(vordid);
    case 6: {
    vorgid = inLines.readLine();
    System.out.println(vorgid);
         case 7: {
    vm1 = inLines.readLine();
    System.out.println(vm1);
         case 8: {
    vs1 = inLines.readLine();
    System.out.println(vs1);
         case 9: {
    vqty1 = inLines.readLine();
    System.out.println(vqty1);
         case 10: {
    vm2 = inLines.readLine();
    System.out.println(vm2);
         case 11: {
    vs2 = inLines.readLine();
    System.out.println(vs2);
    case 12: {
    vqty2 = inLines.readLine();
    System.out.println(vqty2);
    case 13: {
    vm3 = inLines.readLine();
    System.out.println(vm3);
         case 14: {
    vs3 = inLines.readLine();
    System.out.println(vs3);
    case 15: {
    vqty3 = inLines.readLine();
    System.out.println(vqty3);
    case 16: {
    vm4 = inLines.readLine();
    System.out.println(vm4);
    case 17: {
    vs4 = inLines.readLine();
    System.out.println(vs4);
    case 18: {
    vqty4 = inLines.readLine();
    System.out.println(vqty4);
    f1.close();
    FileWriter f2=new FileWriter("c:/testout.txt");
    String t;
    t=ServerIntf.add(vcno,vpin,vamt,vordid,vptype,vreq,vorgid,vm1,vs1,vqty1,vm2,vs2, vqty2,vm3,vs3,vqty3,vm4,vs4,vqty4);
    String str1 = " >>";
    str1 = t + str1;
    f2.write(str1);
    System.out.println('\n'+"c:/testout.txt File updated");
    f2.close();
    catch(Exception e)
    System.out.println("Error " +e);

    Yes, ServerIntf is the interface type. The instance serverIntf. You declared it somewhere at the top of the routine.
    So what you must do is call t=serverIntf.add(...)This is probably just a mistype.

  • HT1689 Using my iPad how can I make paypal my payment method on iTunes??

    Using my iPad how can I make paypal my payment method on iTunes??

    When you open a word document, on the bottom of the keyboard next to the spacebar there should be a microphone. Tap it and speak what you want into it then tap it again to stop.

  • How can I make my small picture disappear while on Facetime, how can I make my small picture disappear while on Facetime

    how can I make my small picture disappear temporarily
    while on Facetime

    You cannot. That is what the other person is seeing.

  • Can i make routers of different series while making my end node of different series

    Hi everyone,
    I am using 9792 programmable ethernet gateway and nodes (3202,3212,3226). rightnow, i am acquiring my data from 3202(end node). can i make the routers using the node 3212 (thermocouple node) and 3226 (RTD/Voltage node). will this work or i have to make routers of the same series
    Replay ASAP.

    Hi abh,
    Any node can be configured as a router and can route signals from any other node regardless of the node type. 
    Cheers,
    Brian A
    R & D Product Support Engineer | WSN/Network DAQ/Wireless
    National Instruments

  • Can I make a character in color while the background is in B&W?

    Hey,
    I am making a short film right now where I need the background to be in black and white while the main character remains in color. Is there any possible way that I can do this in FCE?
    -CSK

    Yes.
    Search this forum for Pleasantville.
    There is plenty of info available.
    Al

  • How do you make a static reference to a method?  I've included code.

    I'm sorry but this is a cross post. This should be here but it is also in the 100% pure Java forum. It won't happen again.
    Now...
    Why doesn't this work? How do I use the method add(int a, int b)?
    ERROR - "Can't make static reference to method int add(int, int) in testClass"
    interface testInterface{   
        static String sString = "TESTING";   
        public int add(int a, int b);
    class testClass implements testInterface{
        public int add(int a, int b){
            return a+b;      
        public static void main(String argv[]){
            int sum = add(3,4);    // here's the error
            System.out.println("test");
            System.out.println( sum );   
    }Again, I apologize for the cross post.

    hi,
    this seems to be pretty easy, isn't it?
    Oh c'mon! You try to invoke a non-static method from within a static method. Solution: Create a specific instance of class testClass:
    testClass test=new testClass();
    test.add(3,4);best regards, Michael

  • How do I make a static reference to a method?  Sample Code Included

    Why doesn't this work? How do I use the method add(int a, int b)?
    ERROR - "Can't make static reference to method int add(int, int) in testClass"
    interface testInterface{
        static String sString = "TESTING";
        int add(int a, int b);
    class testClass implements testInterface{
        public int add(int a, int b){
            return a+b;  
        public static void main(String argv[]){
            int sum = add(3,4);    // here's the error
            System.out.println("test");
            System.out.println( sum );
    }

    Why doesn't this work?Because you can't call a non-static method like add (which operates on the object called this) from a static method like main (for which there is no this).
    There are two ways to fix this:
    (1) In main, create a TestClass object, and call add() for that object:
    class TestClass implements TestInterface {
       public int add(int a, int b) {
          return a+b;
       public static void main(String[] args) {
          TestClass testObject = new TestClass();
          int sum = testObject.add(3, 4);
          System.out.println("test");
          System.out.println(sum);
    }(2) Make add() static. This is the preferred approach, because add() doesn't really need a TestClass object.
    class TestClass implements TestInterface {
       public static int add(int a, int b) {
          return a+b;
       // main is same as the original
    }

  • Compiler thinks getDeclaredFields method is in a static context

    Hi folks,
    I have the following lines of code at the beginning of a non-static method ;
    public void streamOutput(Object objectToStream, boolean autoFlush)
    Class objectClass = objectToStream.getClass();
    Field[] fieldArray = Class.getDeclaredFields();
    When using javac to compile this code I get the following error ;
    Can't make static reference to method java.la
    ng.reflect.Field getDeclaredFields()[] in class java.lang.Class.
    Field[] fieldArray = Class.getDeclaredFields();
    ^
    The thing is that I can't see how javac thinks that the method is being called within a static context. The method is non-static, the containing class in non-static, and I even have a reference to 'this' later on inside the method which the compiler accepts ( which it surely wouldn't if I was working within a static context ).
    Any ideas what is wrong ?

    The line "Class.getDeclaredFields();" is written as if the method getDeclaredFields() is a static method. It isn't. You have to use an instance of some class. Maybe what you need is objectClass.getClass().getDeclaredFields();

  • STATIC REFERENCE

    THE CODE IS ATTACHED. I NEED TO SOLVE THE ERROS.
    THIS IS THE ERRORS AFTER COMPILE
    C:\My Documents\Java\Assignment\Assign3\PwdClient.java:47: Can't make static reference to method void setLayout(java.awt.LayoutManager) in class java.awt.Container.
                        setLayout(new FlowLayout());
                        ^
    C:\My Documents\Java\Assignment\Assign3\PwdClient.java:48: Can't make static reference to method void setBackground(java.awt.Color) in class java.awt.Component.
                        setBackground(Color.green);
                        ^
    C:\My Documents\Java\Assignment\Assign3\PwdClient.java:53: Can't make static reference to method java.awt.Component add(java.awt.Component) in class java.awt.Container.
                        add(l);
                        ^
    C:\My Documents\Java\Assignment\Assign3\PwdClient.java:58: Can't make static reference to method java.awt.Component add(java.awt.Component) in class java.awt.Container.
                        add(name);
                        ^
    C:\My Documents\Java\Assignment\Assign3\PwdClient.java:61: Can't make a static reference to nonstatic variable tf_name in class PwdClient.
                        tf_name = new TextField(20);
                        ^
    5 errors
    //HERE IS THE CODE
    import java.io.*;
    import java.net.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    class PwdClient extends Frame
         private TextField tf_name;
         private TextField tf_password;
         private String input_str_name;
         private String input_str_password;
         private String str_to_pass;
         //private String str;
         //private String str_name;
         //private String str_password;
         //private Button button1;
         private final static int buffsize = 512;
         public static void main(String args[])
              try
                        String server = args[0];
                        int port = Integer.parseInt(args[1]);
                        //create socket
                        Socket s = new Socket(server, port);
                        //readresult from server
                        InputStream is = s.getInputStream();
                        DataInputStream dis = new DataInputStream(is);
                        OutputStream os = s.getOutputStream();
                        DataOutputStream dos = new DataOutputStream(os);
                        InputStreamReader isr = new InputStreamReader(System.in);
                        BufferedReader br = new BufferedReader(isr);
                        //Frame frm = new Frame( "Client Server" );
                        setLayout(new FlowLayout());
                        setBackground(Color.green);
                        String str = "Enter Name and Password:";
                        Label l= new Label( str, Label.CENTER);
                        l.setBackground(Color.red);
                        add(l);
                        String str_name = "Name:";
                        Label name = new Label( str_name, Label.CENTER);
                        name.setBackground(Color.red);
                        add(name);
                        //Name textfield User input for name
                        tf_name = new TextField(20);
                        add(tf_name);
                        String str_password = "Password:";
                        Label password = new Label( str_password, Label.CENTER);
                        password.setBackground(Color.red);
                        add(password);
                        //Password Textfield (user input for password)
                        tf_password = new TextField(20);
                        tf_password.setEchoChar('*');
                        add(tf_password);
                        Button Button1 = new Button("Submit");
                        add(Button1);
                        Button1.addActionListener( new ActionListener()
                             public void actionPerformed(ActionEvent ae)
                                  input_str_name = tf_name.getText();
                                  tf_name.setText(new String(""));
                                  input_str_password = tf_password.getText();
                                  tf_password.setText(new String(""));
                                  str_to_pass = input_str_name+"/"+input_str_password;
                                  dos.writeBytes(str_to_pass);
                                  String received_from_server = brr.readLine();
                                  l.setText(received_from_server, Label.CENTER);
                        frm.setSize( 300, 200 );
                        frm.show();
                        frm.addWindowListener( new WindowAdapter()
                             public void windowClosing(WindowEvent e)
                             { System.exit(0);}
              }//closes the try
              catch(Exception e){}

    Put the code you have in static main in a constructor, or make an instance of PwdClient in main that you can call those methods on. You can't call non-static methods directly from a static method (main). You need to instantiate the class.
    public class PwdClient extends JFrame {
      public PwdClient() {
        // the code from your main method
      public static void main(String[] args) {
        new PwdClient(); // makes a new instance of PwdClient
    }

  • How to make the Reference field in the in Accounting Tab a required field.

    Hi,
    Can i make the REference field in the Accounting tab required. I want to enter contract number from RE in that field.
    Thank you.
    Rachelle

    Hi,
    Goto t.code OVA2 select A-sales header and then click on procedures in the left--> select the procedure in which you want to incolude this field and new entries the table name is VBAK and field is XBLNR for the reference field in the accouting tab and accordingly assign the status 01 or 02 as per your requirement.
    Then you need to assign the incompletion procedure to the sales document type in VOV8.
    Regards,
    Gopal.

  • Name space conflict between static and instance method

    Hello,
    there seems to be a very unfortunate name space conflict between static and instance method name. Consider:
    static String description() that should return a description of a class.
    String description() that should return a description of an instance.
    That seems to confuse the compiler. How come? Static and instance methods don't have anything to get confused about.
    Thanks for any insights :-)

    Umm...jeez.
    It's not a bug, it's the way it's supposed to be.
    Since a static method can be called the same way an instance method
    A instance = new A();
    A.staticMethod();
    instance.staticMethod();it's not allowed.
    Also in the class, you can call
    public void myMethod() {
          instanceMethodInClass();        // You don't need this, it's superfluous
          staticMethodInClass();          // You don't need the class name.
    }If you didn't understand, then just accept the fact that it is so. Some day you'll understand it.

  • Unlimited World Called Skype can't make any call, ...

    I can't make any call. It says failed when I use subscription in Unlimited world called  please kindly fix it . Can't make any call for a while feeling annoyed.

    I can't make any call mobile and landline in Thailabd

  • Error: Cannot make a static reference to the non-static method

    Below is a java code. I attempt to call method1 and method2 and I got this error:
    Cannot make a static reference to the non-static method but If I add the keyword static in front of my method1 and method2 then I would be freely call the methods with no error!
    Anyone has an idea about such error? and I wrote all code in the same one file.
    public class Lab1 {
         public static void main(String[] args) {
              method1(); //error
         public  void method1 ()
         public  void method2 ()
    }

    See the Search Forums at the left of the screen?
    If you had searched with "Cannot make a static reference to the non-static method"
    http://search.sun.com/search/onesearch/index.jsp?qt=Cannot+make+a+static+reference+to+the+non-static+method+&rfsubcat=siteforumid%3Ajava54&col=developer-forums
    you would have the answer. Almost every question you will ask has already been asked and answered.

  • How can I make a "Keyboard Lock" while iTunes is playing?

    How can I make a "Keyboard Lock" while iTunes or other apps are playing? They should work/play on, screen should still be on, only keyboard is (maybe Password-) locked.
    Thanx in adv, tttimo

    Menu > Apple > System Preferences > Keyboard > Keyboard Shortcuts Application Shortcuts > + > Application: Browse to Pages > Menu Title: Footnote > Keyboard Shortcut: hit combination of keys > Add
    You will need to restart Pages but should then see it next to your Menu item.
    Peter

Maybe you are looking for

  • Is it possible to see who has saved progress on a web form, but not submitted?

    We are using FormsCentral for our application process, and we are allowing applicants to save forms. Is it possible to see who has started a web form, and saved it, but hasn't submitted yet? Maybe by cc'ing the email notificiation they get to an admi

  • POS systems and SAP Business One

    Does anyone know of any Point-of-sale (POS) systems that can work together with our SAP Business one application in retail stores. Please can you advise us on our available POS options with SAP Business One for retail stores. Also would there be a ne

  • Settings Badge notification

    i have an iPad air. Since the iOS 8 update, I have a badge notification on my settings and cannot get rid of it. I have gone through all the settings, restored from iCloud, restored the iPad from iTunes, turned off iCloud drive, and have turned off a

  • Category dependent time functionality in Reporting

    Hi Experts, I have this kinda requirement in my reporting like : In my category I have members like C1,C2,C3 where across C1 I maintained april 2014,may2014 across C2 and June 2014 across C3. So what I want in my reporting is when I choose category a

  • What command should I add to make some of one prefix to go out through particular ISP ?

    I'm running a BGP routing in my network. I have 2 Cisco 7507 .I have 4M to ISP A and 4M to ISP B.One router for one ISP. Currently , I have 3 prefixes such as below :- <p>192.168.14.0/24 <p>192.168.15.0/24 <p>192.168.16.0/24 <p>The best routes always