0

This code:

package agendaweb;

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

public class AgendaWeb {


public class Agenda extends JFrame implements ActionListener{

   //Swing
   private JMenuBar barra;
   private JMenu menu1;
   private JMenuItem mi1, mi2, mi3;
   private JTextField camponuevonombre, camponuevotelefono, campobuscarnombre;
   private JLabel etiquetanuevonombre, etiquetanuevotelefono, imprimenombre, imprimetelefono, nombreprograma, nombreautor, numeroversion, dibusca;
   private JButton boton, botonnuevo, botonbusca;
   //Archivo
   //File archivo = new ("C://pruebas//exportacion.txt");
   Formatter nuevoarchivo;
   Scanner x;

   //Metodo constructor
   public Agenda(){
       setLayout(null);

       nombreprograma = new JLabel ("Agenda Telefónica");
       nombreprograma.setBounds(150,30,180,30);
       add(nombreprograma);

       nombreautor = new JLabel ("Fulanito Mengano Exposito");
       nombreautor.setBounds(150,60,180,30);
       add(nombreautor);

       numeroversion = new JLabel ("Version 0.1");
       numeroversion.setBounds(150,90,180,30);
       add(numeroversion);
   //Menu

       barra = new JMenuBar();
       setJMenuBar(barra);
       menu1 = new JMenu("Archivo");
       barra.add(menu1);
       mi1 = new JMenuItem("Nuevo");
       mi1.addActionListener(this);
       menu1.add(mi1);
       mi2 = new JMenuItem("Nuevo");
       mi2.addActionListener(this);
       menu1.add(mi2);
       mi3 = new JMenuItem("Nuevo");
       mi3.addActionListener(this);
       menu1.add(mi3);


   }

   public void actionPerformed(ActionEvent e){
       Container f=this.getContentPane();
       if(e.getSource()==mi1){
           nombreprograma.setVisible(false);
           nombreautor.setVisible(false);
           numeroversion.setVisible(false);

           //Formulario

           etiquetanuevonombre = new JLabel("Nuevo Nombre");
           etiquetanuevonombre.setBounds(0,0,180,30);
           add(etiquetanuevonombre);
           etiquetanuevonombre.setVisible(true);

           camponuevonombre = new JLabel("Nuevo Nombre");
           camponuevonombre.setBounds(200,0,180,30);
           add(camponuevonombre);
           camponuevonombre.setVisible(true);

           etiquetanuevotelefono = new JLabel("Nuevo Nombre");
           etiquetanuevotelefono.setBounds(0,0,180,30);
           add(etiquetanuevotelefono);
           etiquetanuevotelefono.setVisible(true);

           camponuevotelefono = new JLabel("Nuevo Nombre");
           camponuevotelefono.setBounds(200,50,180,30);
           add(camponuevotelefono);
           camponuevotelefono.setVisible(true);

           botonnuevo = new JButton("Crear");
           botonnuevo.setBounds(200,100,180,30);
           add(botonnuevo);
           botonnuevo.addActionListener(this);
           botonnuevo.setVisible(true);

       } 
       if(e.getSource()==mi2){


       } 
       if(e.getSource()==mi3){
          System.exit(0);

       } 

       if(e.getSource()==botonnuevo){
          try{
              Class.forName("com.mysql.jdbc.Driver");
              Connection con = DriveManager.getConnection("");
              Statement
              estado.executeUpdate("INSERT INTO agenda VALUES ()");
          } catch(SQLException ex){
              System.out.println("Error de MyQSL");

          }

       } 

   }
  }

public static void main(String[] args) {

    Agenda ventana = new Agenda();
    ventana.setBounds(10,20,450,250);
    ventana.setVisible(true);
}

}

Shows this error:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - non-static variable this cannot be referenced from a static context at agendaweb.AgendaWeb.main(AgendaWeb.java:125)

Why this is happening and how to solve it?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ViceNocillator
  • 105
  • 3
  • 11

3 Answers3

1

The problem is your Agenda class is NOT declared as static while your main method is a static method. Try to make your Agenda class static.

user2507946
  • 584
  • 2
  • 9
0

The reason of your error is because Agenda is an attribute of your AgendaWeb class. And in your main method, which is a static method you are trying to access Agenda without creating an instance of AgendaWeb. Only static variables can be accessed without an instance, hence it is throwing an error.

I am not sure why do you need AgendaWeb class, this does not look to be a great design to me. So simply take it off, if it is not required. But if you really need that, then you need to mark either your Agenda class as static or you need to create and instance of AgendaWeb class to access the Agenda as an attribute of it.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • Thanks that's the key. I've nust forget to remove the AgendaWeb class at the beginning. Now that I've erased what was useless and changed the names it's going fine. Thanks for your answer! – ViceNocillator Aug 17 '13 at 14:54
  • @ViceNocillator Glad to help :-) If you find my answer helpful, simply accept it. Accepting the answer generally helps other facing the same problemt. – Juned Ahsan Aug 17 '13 at 15:54
0

Your code does not compile because of several syntax errors. Beside from that:

"this" refers to the current instance of a class, while a static context like main lives at class level, not instance level.

From inside a static context it is impossible to use non-static variables. You should create an instance of your class and use that one.

Michael
  • 291
  • 1
  • 3
  • 13