Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6694729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:08:08+00:00 2026-05-26T06:08:08+00:00

So this is the code I’m working on. Its just a practice and it’s

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:08:09+00:00Added an answer on May 26, 2026 at 6:08 am

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code illustrates something that I think should be treated as bad practice, and
This code does not seem to compile, I just need to write something to
this code was just working! but for some reason it stopped to work now.
This code gets the text that's in a certain column of a table row:
This code doesn't work, anyone have any recommendations? Perhaps I did something wrong? Anyone
This code give me this error: c.apply is not a function All code works
This code displays hello in chinese char buffer[10]=hello; AfxMessageBox((LPCTSTR)buffer); while this code displays it
This code in JS gives me a popup saying i think null is a
This code is from Prototype.js . I've looked at probably 20 different tutorials, and
this code always returns 0 in PHP 5.2.5 for microseconds: <?php $dt = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.