-2

My Java compiler returns the following error message

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

on the line String Cliente4 = UtilMainApp.Cliente.

I have the following classes: UtilMainApp.java

package uk.co.mmscomputing.util;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.beans.*;
import javax.swing.*;

import uk.co.mmscomputing.util.log.LogBook;

abstract public class UtilMainApp extends JApplet{

  private Properties properties=new Properties();
  private File       propertiesFile;
  private JFrame     frame = null;

  public String Cliente = getParameter("id"); // I need use this variable


... etc...

and ImageTab.java:

package uk.co.mmscomputing.application.imageviewer;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.print.*;
import java.io.*;
import java.util.*;                // as of 1.5.0 java.util has class Scanner
import javax.imageio.*;
import javax.imageio.stream.*;
import java.beans.*;
import java.net.URL;
import java.net.URLConnection;

import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.imageio.metadata.*;

import uk.co.mmscomputing.util.UtilMainApp;
import uk.co.mmscomputing.util.JarImageIcon;
//import uk.co.mmscomputing.imageio.*;
import uk.co.mmscomputing.image.operators.*;

public class ImageTab extends JPanel implements PropertyChangeListener{

  static public final String fileOpenID="uk.co.mmscomputing.file.open.dir";
  static public final String fileSaveID="uk.co.mmscomputing.file.save.dir";

  protected Properties   properties;
  protected JTabbedPane  images;
  protected JFileChooser openfc;
  protected JFileChooser savefc;


 ... etc...

    //UtilMainApp mc = new UtilMainApp(); //This is the part when I compile have an error
    String Cliente4 = UtilMainApp.Cliente;
    String Usuario;
    Usuario = Cliente4;
    JOptionPane.showMessageDialog(null, Usuario);

...etc...

How is this caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 2
    Your question title has been fixed to reflect the concrete problem. Now please look at the "Related" list on the right hand side (and also [here](https://www.google.com/search?q=%22non-static%20method%22%20%22cannot%20be%20referenced%20from%20a%20static%20context%22)). – BalusC Sep 24 '12 at 16:07

2 Answers2

2

String Cliente4 = UtilMainApp.Cliente;

It seems that Cliente is a Non-static Variable, so it must be accessed by making an object of UtilMainApp Class.

Eg:

UtilMainApp util = new UtilMainApp();

String Cliente4 = util.Cliente;
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • Hi thanks I try this but the UtilMainApp its a abstract class and I can´t be instantiated – Diego Albarracin Sep 24 '12 at 16:15
  • @DiegoAlbarracin Ok... if its an abstract class `you need to extend that UtilMainApp class`, and then access the Cliente field of that extended Class, cause its very important to know that `Fields are Not Polymorphic...` – Kumar Vivek Mitra Sep 24 '12 at 16:17
1

The error is here, where you are referencing a non-static variable in a static way. UtilMainApp is a class, not a property. So you're referencing Cliente as if there is only one of them per class (static) when it is not defined that way.

String Cliente4 = UtilMainApp.Cliente;

To fix this problem, you have to pass an instance of UtilMainApp into the constructor for your ImageTab class, so that you will have the reference to use to get the non-static property Cliente. I'll also note that this property cannot be set to static since it is initialized through the non-static call to getProperty() in the super class.

public String Cliente = getParameter("id"); // I need use this variable

One other note - it is good practice to name all properties beginning with lowercase letters. This is to make sure that you confuse class names with property names. Your code is quite difficult to follow because you do not follow this convention.

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98