Ayuda con Null Pointer Exception
Publicado por Jose Ignacio (3 intervenciones) el 06/12/2015 20:30:02
Hola, soy un novato en esto de programacion, pero estoy intentando aprender a base de videos y tutoriales.
La cuestion es que estoy intentanto hacer un pequeño sistema de manejo de base de datos y he seguido las instrucciones de un tutorial de internet.
el problema viene cuando lo ejecuto. me da el siguiente error ....
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
he revisado todo y no se en que estoy fallando.....
si alguien me puede ayudar por favor ....
tengo dos archivos en el paquete de proyecto uno es la clase main:
La base de datos esta hecha con phpmyadmin y tiene 2 tablas
La tabla Alumnos tiene 5 campos
Id_alumno int(11)
nombre varchar(15)
apellidos varchar(15)
telefono int(11)
grupo varchar (25)
y la otra tabla se llama grupos con 2 campos
id_grupo int(11)
grupo varchar(25)
Si alguien puede ayudarme, estoy muy liado con esto.
Muchas gracias ¡¡
La cuestion es que estoy intentanto hacer un pequeño sistema de manejo de base de datos y he seguido las instrucciones de un tutorial de internet.
el problema viene cuando lo ejecuto. me da el siguiente error ....
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
he revisado todo y no se en que estoy fallando.....
si alguien me puede ayudar por favor ....
tengo dos archivos en el paquete de proyecto uno es la clase main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package alumnos;
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.table.*;
import alumnos.Main.*;
import static alumnos.Main.EnlAlu;
import static alumnos.Main.Enlace;
import static alumnos.Main.alu;
/**
*
* @author Familia
*/
public class frmMantAlum extends javax.swing.JFrame
{
static Connection conn=null;
static Statement st=null;
static ResultSet rs=null;
DefaultTableModel dtm=new DefaultTableModel();
/**
* Creates new form frmMantAlum
*/
public frmMantAlum()
{
initComponents();
activaBotones(true,false,false,false);
String titulos[]={"Codigo","Nombre","Apellidos","Telefono","Grupo"};
dtm.setColumnIdentifiers(titulos);
tablaAlumnos.setModel(dtm);
// SetSize(530,230);
// SetLocation(200,200);
}
public final void activaBotones(boolean n,boolean e,boolean m,boolean g)
{
btnNuevo.setEnabled(n);
btnEliminar.setEnabled(e);
btnModificar.setEnabled(m);
btnGuardar.setEnabled(g);
}
public void limpiarObjetos()
{
txtCodAlu.setText("");
txtNom.setText("");
txtApe.setText("");
txtTel.setText("");
txtGru.setText("");
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
private void txtCodAluActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {
int resp;
resp=JOptionPane.showConfirmDialog(null,"¿Desea Eliminar el registro?","pregunta",0);
if (resp==0)
{
try
{
conn=Main.Enlace(conn);
st=Main.alu(st);
rs=Main.EnlAlu(rs);
int cod;
String comando;
cod=Integer.parseInt(txtCodAlu.getText());
comando="UPDATE Alumnos"+String.valueOf(cod);
st.executeQuery(comando);
conn.close();
activaBotones(true,false,false,false);
limpiarObjetos();
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null,"Error "+e.toString());
}
}
// TODO add your handling code here:
}
private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {
String b;
if (btnNuevo.isEnabled())
{
try
{
conn=Enlace(conn);
rs=EnlAlu(rs);
b=txtCodAlu.getText();
boolean encuentra=false;
while(rs.next())
{
if (b.equals(rs.getString(1)))
{
txtNom.setText((String)rs.getString(2));
txtApe.setText((String)rs.getString(3));
txtTel.setText((String)rs.getString(4));
txtGru.setText((String)rs.getString(5));
this.activaBotones(true,true,true,false);
encuentra=true;
break;
}
}
if (encuentra==false)
{
txtCodAlu.setText("No existe");
txtCodAlu.requestFocus();
}
}
catch (SQLException e)
{
JOptionPane.showMessageDialog(null,"Error BD"+e.getMessage());
}
}
}
private void btnVerActionPerformed(java.awt.event.ActionEvent evt) {
try
{
this.setSize(530,500);
int f, i;
conn=Enlace(conn);
rs=EnlAlu(rs);
String datos[]=new String[5];
f=dtm.getRowCount();
if (f>0)
for(i=0;i<f;i++)
dtm.removeRow(0);
while(rs.next())
{
datos[0]=(String)rs.getString(1);
datos[1]=(String)rs.getString(2);
datos[2]=(String)rs.getString(3);
datos[3]=(String)rs.getString(4);
datos[4]=(String)rs.getString(5);
dtm.addRow(datos);
}
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null,"Error en BD"+e.toString());
}
// TODO add your handling code here:
}
private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {
limpiarObjetos();
txtCodAlu.setEnabled(false);
txtNom.requestFocus();
activaBotones(false,false,false,true);
// TODO add your handling code here:
}
private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {
int resp;
resp=JOptionPane.showConfirmDialog(null,"¿Desea Grabar el registro?", "Pregunta",0);
if (resp==0)
{
try
{
conn=Enlace(conn);
st=alu(st);
rs=EnlAlu(rs);
String nom, ape, tele, grup, comando;
nom=txtNom.getText();
ape=txtApe.getText();
tele=txtTel.getText();
grup=txtGru.getText();
comando="INSERT INTO Alumnos (nombre,apellidos,telefono,grupo) VALUES ";
comando=comando+"('"+nom+"','"+ape+"','"+tele+"','"+grup+"',1)";
st.executeUpdate(comando);
conn.close();
activaBotones(true,false,false,false);
limpiarObjetos();
txtCodAlu.setEnabled(true);
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null,"Error "+e.toString());
}
}
}
private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {
int resp;
resp=JOptionPane.showConfirmDialog(null,"¿Desea Modificar el registro?", "Pregunta",0);
if (resp==0)
{
try
{
conn=Enlace(conn);
st=alu(st);
rs=EnlAlu(rs);
String nom, ape, tele, grup, comando;
int cod;
cod=Integer.parseInt(txtCodAlu.getText());
nom=txtNom.getText();
ape=txtApe.getText();
tele=txtTel.getText();
grup=txtGru.getText();
comando="UPDATE Alumnos SET nombre='"+nom+"','"+ape+"','"+tele+"','"+grup+"',";
comando=comando+" where id_alumno="+String.valueOf(cod);
st.executeUpdate(comando);
conn.close();
activaBotones(true,false,false,false);
limpiarObjetos();
txtCodAlu.setEnabled(true);
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null,"Error "+e.toString());
}
}
// TODO add your handling code here:
}
La base de datos esta hecha con phpmyadmin y tiene 2 tablas
La tabla Alumnos tiene 5 campos
Id_alumno int(11)
nombre varchar(15)
apellidos varchar(15)
telefono int(11)
grupo varchar (25)
y la otra tabla se llama grupos con 2 campos
id_grupo int(11)
grupo varchar(25)
Si alguien puede ayudarme, estoy muy liado con esto.
Muchas gracias ¡¡
Valora esta pregunta


0