0

So this is the code I'm working on. Its just a practice and it's something that's bugging me for a while.The problem is that I'm trying to apply the passed parameter(UDPServer server) in the second class to a global variable for the class(myserv) so I could use it in the run method. The thing is that it says that server is not NULL (which is OK it works) but myserv is NULL and it wouldnt change... Its probably some stupid mistake but I cant seem to find it.

package Server;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class UDPServer{
  JFrame frame;
  JPanel panel;
  JButton button1,button2;
  JTextArea area;
  JScrollPane pane;
  StartThread thread1;
  UDPServer u;

  public static void main(String[] args) {
  UDPServer u = new UDPServer();
  }
  public UDPServer(){
  frame = new JFrame("Text Server");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setUndecorated(true);
  frame.getRootPane()
  .setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  panel = new JPanel();
  panel.setLayout(null);
  area = new JTextArea();
  area.setEditable(false);
  button1 = new JButton("Start");
  button1.setBounds(210, 10, 75, 40);
  button1.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent ae){
  thread1 = new StartThread(u);
  }
  });
  panel.add(button1);
  button2 = new JButton("Stop");
  button2.setBounds(300, 10, 75, 40);
  button2.addActionListener(new ActionListener(){
  public void actionPerformed (ActionEvent ae){
  thread1.thread.interrupted();
  thread1.socket.close();
  area.append("Server is stopped\n");
  button1.setEnabled(true);
  button2.setEnabled(false);
  }
  });
  button2.setEnabled(false);
  panel.add(button2);
  pane = new JScrollPane(area);
  pane.setBounds(10, 60, 365, 250);
  panel.add(pane);
  frame.add(panel);
  frame.setSize(400, 400);
  frame.setVisible(true);
  }
}

That was the first class and thats the second one.

package Server;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class StartThread implements Runnable{

    DatagramSocket socket;
    private final UDPServer myserv;
    Thread thread;
    StartThread(UDPServer server){
        myserv = server;
        thread = new Thread(this);
        thread.start();
        myserv.button1.setEnabled(false);
        myserv.button2.setEnabled(true);
        }

    public void run(){
        try{
            byte[] buffer = new byte[1024];
            int port = 8080;
            try{
                socket = new DatagramSocket(port);
                while(true){
                    try{
                        myserv.area.append("Server is started\n");
                        //Receive request from client
                        DatagramPacket packet = 
                            new DatagramPacket(buffer, buffer.length );
                        socket.receive(packet);
                        InetAddress client = packet.getAddress();
                            int client_port = packet.getPort();
                            myserv.area.append(" Received "
                                    +new String(buffer)+" from "+client);

                    }
                    catch(UnknownHostException ue){}
                }
            }
            catch(java.net.BindException b){}
        }
        catch (IOException e){
            System.err.println(e);
        }
    }
}
1
  • This code is too inter-dependent; you're creating the StartThread in the middle of creating the UDPServer and each relies on the other. Make the code less-coupled. Commented Oct 16, 2011 at 22:03

1 Answer 1

1

This is some ugly code.

For starters, I'd make that UDPServer a separate class.

Here's your problem:

Your class has a private member variable u:

UDPServer u;

You don't initialize it, so the reference is set to null.

Your main method declares a local variable u of the same type.

The two are completely unrelated. You're looking at the "new UPDServer" and imagining that you're initializing the private member variable. That's what is confusing you.

Initialize the UDPServer member variable in a constructor.

Once you do that you'll have another problem: it won't be referable in a static context. Sounds like your next question is all queued up...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.