Java.lang.NullPointerException

Fermé
Juju2606 - 17 mai 2014 à 10:14
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 - 17 mai 2014 à 11:29
Bonjour,
j'ai un probleme avec mon programme qui m'affiche un "java.lang.NullPointerException" je ne sais pas quelle variable je n'ai pas initialisé... Help !

/Creation du package regroupementfinal
package regroupementfinal;

//Importation des librairies
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.nio.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;

//Déclaration des variables globales




class ARDrone extends javax.swing.JFrame implements KeyListener {
//Déclaration des variables
InetAddress inet_addr;
DatagramSocket socket;
int seq = 1;
float speed = (float)0.1;
boolean shift = false;
FloatBuffer fb;
IntBuffer ib;

String at_cmd2 = "";

private JPanel jPanel1;
private JButton jButton1;
private JButton jButton2;
private JButton jButton3;
private JLabel jLabel1;
private JButton jButton5;
private JButton jButton6;
private JButton jButton4;
private JLabel jLabel2;



public ARDrone() {
initComponents();//appel de la methode initialise
}

public ARDrone(String name, String args[]) throws Exception {
super(name);
//Connection avec l'adresse ip de l'ardrone
String ip = "192.168.1.1";

if (args.length >= 1) {
ip = args[0];
}

StringTokenizer st = new StringTokenizer(ip, ".");

byte[] ip_bytes = new byte[4];
if (st.countTokens() == 4){
for (int i = 0; i < 4; i++){
ip_bytes[i] = (byte)Integer.parseInt(st.nextToken());
}
}
else {
System.out.println("Incorrect IP address format: " + ip);
System.exit(-1);
}

System.out.println("IP: " + ip);
System.out.println("Speed: " + speed);

ByteBuffer bb = ByteBuffer.allocate(4);
fb = bb.asFloatBuffer();
ib = bb.asIntBuffer();

inet_addr = InetAddress.getByAddress(ip_bytes);
socket = new DatagramSocket();
socket.setSoTimeout(3000);

send_at_cmd("AT*CONFIG=1,\"control:altitude_max\",\"2000\""); //altitude max 2m

if (args.length == 2) { //Commandline mode
send_at_cmd(args[1]);
System.exit(0);
}

addKeyListener(this);
setSize(320, 160);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

[...]

public void send_at_cmd(String at_cmd) throws Exception {
System.out.println("AT command: " + at_cmd);
byte[] buffer = (at_cmd + "\r").getBytes();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, inet_addr, 5556);
socket.send(packet);
//socket.receive(packet); //AR.Drone does not send back ack message (like "OK")
//System.out.println(new String(packet.getData(),0,packet.getLength()));
}

[...]

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)throws Exception {
System.out.println("Devant");
at_cmd2 = "AT*PCMD=303,1,1036831949,0,0,0";
send_at_cmd(at_cmd2);
}

[...]

Je n'ai pas mis tout le programme car il est bcp trop long mais je pense que le probleme se trouve ici.
Merci de votre aide !

1 réponse

KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
17 mai 2014 à 11:29
Bonjour,

Il faut regarder la trace de l'exception, elle te dira exactement sur quelle ligne le NullPointerException a été trouvé, ça te permettra de trouver la valeur coupable.
0