Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JLabel.setText() not working... 1

Status
Not open for further replies.

mac2k

Technical User
Oct 2, 2000
10
US
Hi everybody:

I'm working on a program that has to change the value of a variable every minute, I start by defining the GUI and a Timer object, to listen when the value must change, by this way:

import ...

public class changeValue extends JFrame implements ActionListener{
private JLabel valVar;
private int delay = 60000;
private Timer myTm;
private contain;

public changeValue(String value){
contain = getContentPane();
valVar = new JLabel(value);
contain.add(valVar);
setSize(200,200);
setVisible(true);
}

public void chgVal(String newVal){
valVar.setText(newVal);
}

public void start(){
//all the timer stuff here...
}

public void main...{
changeValue app = new changeValue("1");
app.start();
}

public void actionPerformed(ActionEvent e){
chgVal("2");
}

}

but when i run this program i recived a Null Pointer Exception, that means i can access this JLabel component properties, can anyone help me? Thanks.

mac2k.
 
I'm not overally skillful at Swing ... but it seems as if the method accepting your action event is hardcoded to change the value to "2" ... is that desired behaviour or just an example ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I'd like to have enough code so I could fool around with this myself. Can you post the whole program?

--Dave
 
Thanks for replying:

yes sedj that was an example

ok, this is the whole code:

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

public class monitor extends JFrame implements ActionListener{
private Container contenedor;
private GridLayout grdM, grd1, grd2;
private JPanel panLbl, panVal;
private JLabel lblMarca1, lblMarca2, lblMarca3, lblMarca4, lblMarca5;
private JLabel lblVal1, lblVal2, lblVal3;
private JLabel lblVal4, lblVal5, lblVal6;
private JLabel lblVal7, lblVal8, lblVal9;
private JLabel lblVal10, lblVal11, lblVal12;
private JLabel lblVal13, lblVal14, lblVal15;
private int retraso = 30000;
private Timer miTimer;

static final String CONTROLADOR = "oracle.jdbc.driver.OracleDriver";
static final String URL = "jdbc:eek:racle:thin:mad:10.15.10.106:1521:Mydb";
static final String USUARIO = "xxxxx";
static final String PSWRD = "11111";
static final String CONSULTA = "select id_1,id_2,id_3 from MyTable";

private DbConnx DbConexion;
private String[][] datosNuevos;

/** Crea una nueva instancia de monitor */
public monitor(String[] texto, String[] valor1, String[] valor2, String[] valor3){
super("Monitor");

contenedor = getContentPane();
grdM = new GridLayout(5,4);
contenedor.setLayout(grdM);

lblMarca1 = new JLabel(texto[0], SwingConstants.CENTER);
lblMarca1.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel lblVal1 = new JLabel("", SwingConstants.CENTER);
lblVal1.setForeground(new Color(255,0,0));
lblVal1.setBorder(BorderFactory.createLineBorder(Color.black));
lblVal1.setText(valor1[0]);
JLabel lblVal2 = new JLabel("", SwingConstants.CENTER);
lblVal2.setForeground(new Color(0,0,255));
lblVal2.setBorder(BorderFactory.createLineBorder(Color.black));
lblVal2.setText(valor2[0]);
JLabel lblVal3 = new JLabel("", SwingConstants.CENTER);
lblVal3.setForeground(new Color(255,255,50));
lblVal3.setBorder(BorderFactory.createLineBorder(Color.black));
lblVal3.setText(valor3[0]);
contenedor.add(lblMarca1);
contenedor.add(lblVal1);
contenedor.add(lblVal2);
contenedor.add(lblVal3);

lblMarca2 = new JLabel(texto[1], SwingConstants.CENTER);
lblMarca2.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel lblVal4 = new JLabel(valor1[1], SwingConstants.CENTER);
lblVal4.setForeground(new Color(255,0,0));
lblVal4.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel lblVal5 = new JLabel(valor2[1], SwingConstants.CENTER);
lblVal5.setForeground(new Color(0,0,255));
lblVal5.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel lblVal6 = new JLabel(valor3[1], SwingConstants.CENTER);
lblVal6.setForeground(new Color(255,255,50));
lblVal6.setBorder(BorderFactory.createLineBorder(Color.black));
contenedor.add(lblMarca2);
contenedor.add(lblVal4);
contenedor.add(lblVal5);
contenedor.add(lblVal6);

lblMarca3 = new JLabel(texto[2], SwingConstants.CENTER);
lblMarca3.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel lblVal7 = new JLabel(valor1[2], SwingConstants.CENTER);
lblVal7.setForeground(new Color(255,0,0));
lblVal7.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel lblVal8 = new JLabel(valor2[2], SwingConstants.CENTER);
lblVal8.setForeground(new Color(0,0,255));
lblVal8.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel lblVal9 = new JLabel(valor3[2], SwingConstants.CENTER);
lblVal9.setForeground(new Color(255,255,50));
lblVal9.setBorder(BorderFactory.createLineBorder(Color.black));
contenedor.add(lblMarca3);
contenedor.add(lblVal7);
contenedor.add(lblVal8);
contenedor.add(lblVal9);

lblMarca4 = new JLabel(texto[3], SwingConstants.CENTER);
lblMarca4.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel lblVal10 = new JLabel(valor1[3], SwingConstants.CENTER);
lblVal10.setForeground(new Color(255,0,0));
lblVal10.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel lblVal11 = new JLabel(valor2[3], SwingConstants.CENTER);
lblVal11.setForeground(new Color(0,0,255));
lblVal11.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel lblVal12 = new JLabel(valor3[3], SwingConstants.CENTER);
lblVal12.setForeground(new Color(255,255,50));
lblVal12.setBorder(BorderFactory.createLineBorder(Color.black));
contenedor.add(lblMarca4);
contenedor.add(lblVal10);
contenedor.add(lblVal11);
contenedor.add(lblVal12);

lblMarca5 = new JLabel(texto[4], SwingConstants.CENTER);
lblMarca5.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel lblVal13 = new JLabel(valor1[4], SwingConstants.CENTER);
lblVal13.setForeground(new Color(255,0,0));
lblVal13.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel lblVal14 = new JLabel(valor2[4], SwingConstants.CENTER);
lblVal14.setForeground(new Color(0,0,255));
lblVal14.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel lblVal15 = new JLabel(valor3[4], SwingConstants.CENTER);
lblVal15.setForeground(new Color(255,255,50));
lblVal15.setBorder(BorderFactory.createLineBorder(Color.black));
contenedor.add(lblMarca5);
contenedor.add(lblVal13);
contenedor.add(lblVal14);
contenedor.add(lblVal15);

setSize(300,180);
setLocation(200,200);
setVisible(true);
}

public void muestraDatos(String[] valor1, String[] valor2, String[] valor3){
lblVal1.setText(""+valor1[0]);
lblVal2.setText(""+valor2[0]);
lblVal3.setText(""+valor3[0]);
lblVal4.setText(""+valor1[1]);
lblVal5.setText(""+valor2[1]);
lblVal6.setText(""+valor3[1]);
lblVal7.setText(""+valor1[2]);
lblVal8.setText(""+valor2[2]);
lblVal9.setText(""+valor3[2]);
lblVal10.setText(""+valor1[3]);
lblVal11.setText(""+valor2[3]);
lblVal12.setText(""+valor3[3]);
lblVal13.setText(""+valor1[4]);
lblVal14.setText(""+valor2[4]);
lblVal15.setText(""+valor3[4]);
}

public void start(){
if (miTimer == null) {
miTimer = new Timer(retraso,this);
miTimer.start();
} else
if (!miTimer.isRunning())
miTimer.restart();
}

public void stop()
{
miTimer.stop();
}

public String[][] obtieneDatos(){
try{
DbConnx DbConexion = new DbConnx(CONTROLADOR, URL, USUARIO, PSWRD, CONSULTA);
DbConexion.setQuery(CONSULTA);

if(DbConexion.noDefilas()==0)
throw new SQLException();

int nc = DbConexion.noDeColumnas();
datosNuevos = DbConexion.llenaArreglo(15, nc);
DbConexion.desconecta();
return datosNuevos;
} catch (SQLException eSql){
eSql.printStackTrace();
} catch (ClassNotFoundException cnf){
cnf.printStackTrace();
}
return datosNuevos;
}

public static void main(String[] args) throws SQLException, ClassNotFoundException{
String areas[] = {"Acceso","Core","Plat&Serv.","Datos","Gestion"};
try{
DbConnx DbConexion = new DbConnx(CONTROLADOR, URL, USUARIO, PSWRD, CONSULTA);
DbConexion.setQuery(CONSULTA);
int nf = DbConexion.noDefilas();
int nc = DbConexion.noDeColumnas();
String datosConsulta[][] = new String[15][nc];
datosConsulta = DbConexion.llenaArreglo(15,nc);

String marcas[] = new String[5];
String valor1[] = new String[5];
String valor2[] = new String[5];
String valor3[] = new String[5];

int ind = 0;
int cnom = 0;
int cuantos = 1;

// revisa que no falten datos en cuyo caso
// los deja en cero
while(ind<5){
if (Integer.parseInt(datosConsulta[cnom][2]) == cuantos){
marcas[ind]=areas[ind];
valor1[ind]=datosConsulta[cnom][1];
cuantos++;
} else {
valor1[ind]="0";
cuantos++;
}

if (Integer.parseInt(datosConsulta[cnom+1][2]) == cuantos){
valor2[ind]=datosConsulta[cnom+1][1];
cuantos++;
} else {
valor2[ind]="0";
cuantos++;
}

if (Integer.parseInt(datosConsulta[cnom+2][2]) == cuantos){
valor3[ind]=datosConsulta[cnom+2][1];
cuantos=1;
} else {
valor3[ind]="0";
cuantos=1;
cnom--;
}

ind++;

if (ind<5){
cnom+=3;
}
}

DbConexion.desconecta();
monitor app = new monitor(marcas, valor1, valor2, valor3);
app.setResizable(false);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.start();
} catch (SQLException eSql){
eSql.printStackTrace();
} catch (ClassNotFoundException eSql){
eSql.printStackTrace();
}
}

public void actionPerformed(ActionEvent e){
String datosNuevos[][] = new String[15][3];
datosNuevos = obtieneDatos();
String valor1[] = new String[5];
String valor2[] = new String[5];
String valor3[] = new String[5];

int ind = 0;
int cnom = 0;
int cuantos = 1;

// revisa que no falten datos en cuyo caso
// los deja en cero
while(ind<5){
if (Integer.parseInt(datosNuevos[cnom][2]) == cuantos){
valor1[ind]=datosNuevos[cnom][1];
cuantos++;
} else {
valor1[ind]="0";
cuantos++;
}

if (Integer.parseInt(datosNuevos[cnom+1][2]) == cuantos){
valor2[ind]=datosNuevos[cnom+1][1];
cuantos++;
} else {
valor2[ind]="0";
cuantos++;
}

if (Integer.parseInt(datosNuevos[cnom+2][2]) == cuantos){
valor3[ind]=datosNuevos[cnom+2][1];
cuantos=1;
} else {
valor3[ind]="0";
cuantos=1;
cnom--;
}

ind++;

if (ind<5){
cnom+=3;
}
}

// just for tracking the timer...
for(int u=0;u<5;u++){
System.out.println(" " + valor1+", " + valor2+", " + valor3);
}
System.out.println("------------------------------");
// here is where i get the error...
muestraDatos(valor1, valor2, valor3);
}
}


 
Hola :p

Could you post the stacktrace of the exception you're getting?

Cheers,
Dian
 
Hi, this is the stacktrace i'm getting, thanks ;)

java.lang.NullPointerException
at monitor.muestraDatos(monitor.java:126)
at monitor.actionPerformed(monitor.java:294)
at javax.swing.Timer.fireActionPerformed(Timer.java:271)
at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:448)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
 
That means either the JLabel is null (what I doubt) or any of the String array passed as parameter to the muestraDatos methos is null (most probable one).

Humm, just one more thought ... where are you instantiating the monitor class? I just can't find it.

Oh, and class names use to begin with capitals (Monitor).

Cheers,
Dian
 
Thanks Dian

I instante the monitor class in the main() class after the calling to the database, i don't think the array is null because i show those values in the ActionPerformed listener, and i can see the values changing, i try to change this strings inside the listener and i got the same result...

mac2k
 
I think I see it!

In your constructor, you instantiate all these JLabels with lines like:

Code:
JLabel lblVal1 = new JLabel("", SwingConstants.CENTER);

In other words, you are creating local variables which go out of scope when you leave the constructor.

Change all such lines to:

Code:
lblVal1 = new JLabel("", SwingConstants.CENTER);
[notice I removed the 'JLabel' from the start of the line].

--Dave
 
Thank you Dave, that's rigth i change those lines and it works!

Now this program will get more complicated because i must show other detail when click over one of the text.

So i'll keep working

thanks to all

mac2k
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top