Witam. Gdy uruchomię tą aplikację, prosty komunikator internetowy, i, po wpisaniu IP, kliknę połącz wyświetla się informacja o błędzie i wyrzuca błąd
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at scom.Okno$2.actionPerformed(ScomApp.java:99)
...
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Poniżej zamieszczam kod programu.
package scom;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class ScomApp {
public static void main(String[] args) throws UnknownHostException {
InetAddress mojeIP= InetAddress.getLocalHost();
Okno okno = new Okno();
okno.setSize(350, 440);
okno.setTitle("Gaduła " + mojeIP);
okno.setDefaultCloseOperation(3);
okno.setVisible(true);
}
}
class Okno extends JFrame implements ActionListener {
JTextField pole, pole1, pole2, pole3;
InetAddress addr;
Socket socket;
boolean test;
PrintWriter out;
JTextArea area, area1, area2, area3;
JButton przycisk, przycisk2;
public static final int PORT = 411;
String ip;
JLabel stan;
public Okno() {
JPanel panel = new JPanel();
panel.setBackground(new Color(34, 145, 255));
pole = new JTextField(15);
area = new JTextArea(20, 25);
panel.add(area);
panel.add(pole);
final JLabel stan = new JLabel(" STAN: Rozłączony");
JButton przycisk = new JButton("Wyślij");
final JButton przycisk2 = new JButton("Połącz");
przycisk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
przycisk2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(przycisk2.getText().equals("Połącz"))
{
String ip=pole.getText();
try
{
addr = InetAddress.getByName(ip);
System.out.println("=Lacze sie z komputerem o adresie : " + ip);
socket = new Socket(ip, PORT);
System.out.println("==Na porcie: " + socket.getPort());
test = true;
}
catch(Exception b)
{
przycisk2.setEnabled(false);
stan.setText(" STAN: disconnected");
System.out.println("Polaczenie jest niemozliwe do zrealizowania ......");
test = false;
}
if (test){
przycisk2.setEnabled(true);
stan.setText(" STAN: connected");
}
else{
przycisk2.setEnabled(false);
stan.setText(" STAN: disconnected");
try{
socket.close();
}
catch(IOException c)
{
System.out.println("Problem z zamknieciem socketu");
}
}
}
}
});
panel.add(przycisk);
panel.add(przycisk2);
panel.add(stan);
getContentPane().add(panel);
}
public void actionPerformed(ActionEvent e) {
}
public class Connection extends Thread
{
Connection(Socket s, JTextArea ta)
{
socket = s;
area = ta;
}
void addText(String s)
{
area.setText(s + "\n" + pole.getText());
java.awt.Toolkit.getDefaultToolkit().beep();
}
public void run()
{
try
{
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
while (true)
{
String str = in.readLine();
try {
if ( str.equals("END"))
{
System.out.println(" Nadeszlo polecenie zamkniecia połączenia !");
break;
}
this.addText("------------------------------");
this.addText(socket.getInetAddress() + " : " + str);
}
catch(NullPointerException e)
{
;
}
}
}
finally
{
System.out.println("Połączenie zostało przerwane ....");
socket.close();
}
}
catch(IOException e)
{
przycisk2.setEnabled(false);
stan.setText(" STAN: disconnected");
}
}
}
public class Server extends Thread
{
boolean working = true;
Server(JTextArea ta)
{
area = ta;
}
void addText(String s)
{
area.setText(s + "\n" + pole.getText());
}
public void end()
{
working = false;
}
public void run()
{
while(working)
{
try
{
ServerSocket s = new ServerSocket(PORT);
try
{
System.out.println("Oczekuje na rozmowe ...");
Socket socket = s.accept();
Connection c = new Connection(socket, area);
c.start();
System.out.println("Rozmowa przyjeta - trwa ! port: " + socket.getPort());
}
catch(Exception e) //IO
{
System.out.println("Blad programu: problem z przyjeciem polaczenia");
}
}
catch(IOException e)
{
;
}
}
}
}
private void sendInformation()
{
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
out.println(pole.getText());
area.setText("------------------------------" + "\n" + pole.getText());
area.setText("Ja:" + pole.getText() + "\n" + pole.getText());
}
catch(IOException e)
{
System.out.println("Problem z wyslaniem informacji ...");
System.out.println(e);
}
}
}