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

  • SEARCH
  • Home
  • 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 8413113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:51:18+00:00 2026-06-10T00:51:18+00:00

I keep getting an NullPointerException, and I dont know why. what the class is

  • 0

I keep getting an NullPointerException, and I dont know why. what the class is supposed to do, is to send whatever I write in my jtextfield as writeUTF, but I keep getting a NullPointerException at

dos = new DataOutputStream(socket.getOutputStream());

The GUI code:

public class GUI {

private JFrame frame = new JFrame("Dryck & Ingrediens"); // GUI
private JTextField jtf = new JTextField();// GUI
private JTextArea jl1 = new JTextArea();// GUI
private JList jl = new JList();// GUI
private JScrollPane js = new JScrollPane(jl);// GUI
private DataOutputStream dos;// ServerHandler
private JLabel lab = new JLabel("Ange dryck");//GUI
private JLabel lab1 = new JLabel("Walid Shams");
private JLabel lab2 = new JLabel("Kushtrim Brahimi");
private HashMap<String, ArrayList<String>> drinkar = null;//Controller
private DataInputStream dis;
private Socket socket;
private ServerHandler serverH;

public GUI() {


    frame.getContentPane().setLayout(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(50, 300, 420, 400);
    frame.setResizable(false);
    frame.setVisible(true);
    js.add(jl);
    js.add(jl1);
    jl1.setEditable(false);
    lab.setBounds(170, 20, 130, 20);
    lab1.setBounds(300, 310, 130, 20);
    lab2.setBounds(300, 330, 130,20);
    jtf.setBounds(130, 40, 150, 40);
    jl.setBounds(50, 90, 150, 200);
    jl1.setBounds(210, 90, 150, 200);
    Container con = frame.getContentPane();
    con.setBackground(Color.cyan);

This is where the error accures and where i get the nullpointerexception everytime i type something in my jtextfield.

    jtf.addKeyListener(new KeyListener() {

        public void keyTyped(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {
        }

        public void keyReleased(KeyEvent e) {

            if (dos == null) {
                if (jtf.getText().length() > 0) {
                    try {
                        dos = new DataOutputStream(socket.getOutputStream());
                        dos.writeUTF(jtf.getText());
                    } catch (IOException ex) {
                        Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else {
                    String[] empty = new String[]{""};
                    jl.setListData(empty);
                }
            }

        }
    }
);


    jl.addListSelectionListener(new ListSelectionListener() {

        public void valueChanged(ListSelectionEvent e) {
             if (jl.getSelectedIndex() != -1) {
                 String item = (String) jl.getSelectedValue();
                 jl1.setText("");
                 for (String ingrediens : drinkar.get(item)) {
                     jl1.append(ingrediens + "\n");
                 }
             }else{
                 jl1.setText("");
             }
        }
    });
    frame.add(jtf);
    frame.add(jl);
    frame.add(jl1);
    frame.add(lab);
    frame.add(lab1);
    frame.add(lab2);
    frame.add(js);
}


// tar emot arrayen, lagrar i ny array och visar i JList
public void getArrayList() {

    String[] arr = null;
    for(int i = 0; i < arr.length; i++){
        arr = serverH.setList();
    }
    jl.setListData(arr);


}

public static void main(String[] args) {
    GUI g = new GUI();

}
}
  • 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-06-10T00:51:19+00:00Added an answer on June 10, 2026 at 12:51 am

    You define an instance variable »serverH«

    private ServerHandler serverH;
    

    but don’t assign any object to it. Therefore you should be facing the NullPointerException. You should assign an object to it, e.g.

    serverH = new ServerHandler();
    

    This should solve your problem regarding the NullPointerException.

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

Sidebar

Related Questions

I have a about screen that has some buttons, but I keep getting NullPointerException
I am trying to write program and keep getting a nullPointerException when I call
I am keep getting NullPointerException error at line JSONObject json = userFunctions.uploadFileData(guid, photoName); in
I keep getting 21002 'java.lang.NullPointerException' errors from Apple when I try to test my
I keep getting this java.lang.NullPointerException on the line where mLoginButton's onClickListener is initiated. I
Im trying to display this listview, but I keep getting a: 07-17 21:14:22.233: ERROR/AndroidRuntime(349):
I've got a hibernate query I'm trying to get working but keep getting an
I keep getting NullPointerException trying to deflate or make invisible the ViewStub from my
I keep getting a NullPointerException when I call the method I created, draw(), in
I keep getting Activity Monitor Job. java.lang.NullPointerException while working with Eclipse 3.6 Has anybody

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.