Error in execution time JAVA [closed]

0

I had recently shown a code that was failing, in the end I was able to solve it with the advice of the community and I want to thank you enough, but nevertheless I run into a problem now, the program pulls the error mentioned in the title at the time of compile, when in high school it was not like that ... I do not know if it's a computer problem that I have In addition to wanting to execute it, the following appears

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1041)
at java.awt.Container.add(Container.java:959)
at javax.swing.JFrame.addImpl(JFrame.java:545)
at java.awt.Container.add(Container.java:365)
at based1.<init>(based1.java:143)
at based1.main(based1.java:218)

the code I show you again is

    import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.sql.*;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;

 public class based1 extends JFrame implements ActionListener{
private JTextField textfield1,textfield2,textfield3,textfield4,textfield5;
private JLabel label1,label2,label3,label4,label5;
private JButton boton1,boton2,boton3,boton4;
private JTextArea texto;

 //variables 
int id;
int aux1=0,aux2=0,aux3=0,aux4=0,aux5=0;
float aux6=0;

String linea="";
String linea1="";
String linea2="";
String linea3="";
String linea4="";
String linea5="";

public void limpiar(){
textfield1.setText("");
textfield2.setText("");
textfield3.setText("");
textfield4.setText("");
textfield5.setText("");

}

public void tabla1() {
Vector nombresColumnas=new Vector();
nombresColumnas.add("MATRICULA");
nombresColumnas.add("NOMBRES Y APELLIDOS");
nombresColumnas.add("EX1");
nombresColumnas.add("EX2");
nombresColumnas.add("EX3");
DefaultTableModel modelo=new DefaultTableModel(nombresColumnas,0);//0
JTable tabla=new JTable();
tabla.setModel(modelo);

try{ Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e1){
System.out.println(e1.getMessage());}

try{
String password ="";
//nombre de la base de datos Acces con extencion *.mdb o *.accdb
String dbName = "curso1.mdb";
//direccion de la base de datos
String bd= System.getProperty ("user.dir") + "\" + dbName + ";PWD=" + password;
Connection conexion = DriverManager.getConnection("jdbc:odbc:;DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ="+bd);
Statement estatuto = conexion.createStatement();
ResultSet rs = estatuto.executeQuery ("select * FROM lista ORDER BY MATRICULA"   );
while (rs.next())

   { 
Vector v;
v= new Vector();
v.add (rs.getString("MATRICULA"));
v.add (rs.getString("NOMBRE"));
v.add (rs.getInt("EX1"));
v.add (rs.getInt("EX2"));
v.add (rs.getInt("EX3"));
modelo.addRow(v);
}

///WHILE
JScrollPane scroll=new JScrollPane (tabla);
this.getContentPane().add (scroll);
scroll.setBounds(350,30,400,300);
scroll.setViewportView(tabla);
scroll.setVisible(true);
estatuto.close();
    conexion.close();
    }catch(SQLException e1) {System.out.println(e1.getMessage());
                          }
this.setTitle("Datos Ingresados Correctamente!!!") ;
}

        public based1() {
             setLayout(null);

        label1=new JLabel("Matricula:");
        label1.setBounds(5,5,100,30);
        add(label1);
        textfield1=new JTextField();
        textfield1.setBounds(65,10,100,20);
        add(textfield1);

        label2=new JLabel("Nombres:");
        label2.setBounds(5,25,100,30);
        add(label2);

        textfield2=new JTextField();
        textfield2.setBounds(65,32,200,21);
        add(textfield2);

        label3=new JLabel("Nota 1:");
        label3.setBounds(5,53,100,30);
        add(label3);
        textfield3=new JTextField();
       textfield3.setBounds(65,55,50,21);
        add(textfield3);

        label4=new JLabel("Nota 2:");
        label4.setBounds(120,53,100,30);
         add(label4);
        textfield4=new JTextField();
        textfield4.setBounds(170,55,50,21);
        add(textfield4);

        label5=new JLabel("Nota 3:");
        label5.setBounds(225,53,100,30);
        add(label5);
        textfield5=new JTextField();
        textfield5.setBounds(280,55,50,21);
        add(textfield5);

        boton1=new JButton("Grabar");
        boton1.setMnemonic('G');
        boton1.setBounds(10,120,100,30);
        add(boton1);
        boton1.addActionListener(this);

        boton2=new JButton("Salir");
        boton2.setMnemonic('S');
        boton2.setBounds(220,120,100,30);
        add(boton2);
        boton2.addActionListener(this);


        boton2=new JButton("Mostrar");
        boton2.setMnemonic('D');
        boton2.setBounds(350,120,100,30);
        add(boton3);
        boton2.addActionListener(this);


        boton2=new JButton("Limpiar");
        boton2.setMnemonic('D');
        boton2.setBounds(500,120,100,30);
        add(boton4);
        boton2.addActionListener(this);
}

     public void actionPerformed(ActionEvent e) {
         if (e.getSource()==boton1) {
             linea="";
             linea1=textfield1.getText();
             linea2=textfield2.getText();
             linea3=textfield3.getText();
             linea4=textfield4.getText();
             linea5=textfield5.getText();
             id=Integer.parseInt(linea1);

try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch(ClassNotFoundException e1) {

        System.out.println(e1.getMessage());
                                   }
if(id>0) {

try{

String password ="";

String dbName= "curso1.mdb";


String bd = System.getProperty("user.dir") + "\" + dbName + ";PWD=" + password;

Connection conexion = DriverManager.getConnection("jdbc:odbc:;DRIVER=Microsoft Acces Driver (*.mdb,*.accdb);DBQ=" +bd);

Statement estatuto= conexion.createStatement();

aux1=Integer.parseInt(linea1);
aux3=Integer.parseInt(linea3);
aux4=Integer.parseInt(linea4);
aux5=Integer.parseInt(linea5);

String q="insert into lista (MATRICULA,NOMBRE,EX1,EX2,EX3)values("+aux1 +", '"+linea2+"',"+aux3+","+aux4+","+aux5+")";

estatuto.executeUpdate(q);
estatuto.close();
conexion.close();

}catch(SQLException e1){

texto.append(e1.getMessage());
}
}
else{
JOptionPane.showMessageDialog(null, "La matricula es incorrecta, debe ser numerica");
}
tabla1();
limpiar();
}

if (e.getSource()==boton2) {

System.exit(0);
    }
    }


public static void main(String[] args) {

based1 formulario1=new based1();
formulario1.setBounds(1,50,850,450);
formulario1.setVisible(true);
formulario1.setTitle("Formulario de ingreso de Datos Java+Acces");
   }

}
    
asked by Karen Liliana Robledo 31.01.2016 в 07:12
source

2 answers

2

It's a matter of reading the stacktrace. Let's review:

  

Exception in thread "main" java.lang.NullPointerException

This line indicates the thread where the problem occurred and the exception or error. In this case, the thread issue is not very relevant, so let's focus on the error: java.lang.NullPointerException . This error happens when you are using a variable with value null and you execute an operation on it. For example:

String nombre = null;
System.out.println("Hola " + nombre.trim());

The code above will throw this exception because the variable nombre has value null . The solution is to make sure that null is not used at the time of executing that line of code. Here is a way to solve it, which is assigning a value to the variable:

String nombre = " Karen :D ";
System.out.println("Hola " + nombre.trim()); //imprime: Hola Karen :D

Now, the second part of the stacktrace is a set of calls to stacked methods that the program executes for its operation. In this case, it tells you in which part of the code the error arose:

  

at java.awt.Container.addImpl (Container.java:1041)
  at java.awt.Container.add (Container.java:959)
  at javax.swing.JFrame.addImpl (JFrame.java:545)
  at java.awt.Container.add (Container.java:365)
  at based1. (based1.java:143)
  at based1.main (based1.java:218)

Apparently, the problem happened in the class java.awt.Container , in the method addImpl , and according to the current compilation of this class, in line 1041 of its corresponding .java file. Since java.awt.Container is a class of the JDK, it does not have much chance to review what happened there because it is not something that we can correct (at least not for this case). To do this, you should inspect more about the stack of method calls to find a line on which we work. For this case, the winning line is:

  

at based1. (based1.java:143)

Why is this the line we should review? Because this line of code belongs to the class we are developing and therefore we have control on what's there.

Checking line 143 of your file based1.java (which you certainly put the entire file, facilitating its detection), is:

boton2=new JButton("Mostrar");
boton2.setMnemonic('D');
boton2.setBounds(350,120,100,30);
add(boton3); //<-- esta es la línea 143
boton2.addActionListener(this);

When reviewing the code, the variable boton3 was never initialized, so there's the error.

What should be done? I get the impression that you were wrong to declare the variable in the previous lines, based on a larger fragment of the code:

//líneas anteriores, acá veo que inicializas y utilizas boton2
boton2=new JButton("Salir");
boton2.setMnemonic('S');
boton2.setBounds(220,120,100,30);
add(boton2);
boton2.addActionListener(this);


//acá parece que copiaste/pegaste/trataste de adaptar el código anterior
//y se te olvidó utilizar boton3 en lugar de boton2
boton2=new JButton("Mostrar");
boton2.setMnemonic('D');
boton2.setBounds(350,120,100,30);
add(boton3);
boton2.addActionListener(this);

So it's a matter of replacing the used variable:

boton3=new JButton("Mostrar");
boton3.setMnemonic('D');
boton3.setBounds(350,120,100,30);
add(boton3);
boton3.addActionListener(this);

Apparently you have a similar situation with boton4 after the problem with boton3 , but I'm sure that with this explanation you can solve it.

    
answered by 31.01.2016 / 07:59
source
1

An exception NullPointerException means that some of the variables you are using is null but you are trying to do an operation with it.

If you look at the error stack you will see what happens on line 143, which is add(boton3) within this part:

boton2=new JButton("Mostrar");
boton2.setMnemonic('D');
boton2.setBounds(350,120,100,30);
add(boton3);
boton2.addActionListener(this);

The problem is that you are initializing variable boton2 but then you try to add boton3 . Change the code to:

boton3=new JButton("Mostrar");
boton3.setMnemonic('D');
boton3.setBounds(350,120,100,30);
add(boton3);
boton3.addActionListener(this);

And the same goes for variable boton4

    
answered by 31.01.2016 в 08:01