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 7576557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:57:03+00:00 2026-05-30T16:57:03+00:00

Im new in Java socket and i try to develop a small game through

  • 0

Im new in Java socket and i try to develop a small game through socket, everything is ok on server but in client , i got trouble when i create a list of JLabel to show a letter “_” for each letter of word

Ok i show my code you will see what trouble i got.

my Snippett code like this:

List<JLabel>labels;

 private void getWordLabels(String word){
        char[] chars  = word.toCharArray();
        System.out.println(chars);
        int gapBetweenLetter = 300/chars.length;
        labels = new ArrayList<JLabel>();

        for(int i =0; i< chars.length ;i++){

           // JLabel l = new JLabel("_");
            labels.add(new JLabel());
            labels.get(i).setText("_");
            //this.add(l);
            //l.setBounds(10, 10, 10, 10);
            //l.show();
            this.removeAll();
            this.revalidate();
            this.repaint();
            labels.get(i).setBounds((i*gapBetweenLetter) + 10, 100, 50, 50);

            this.add(labels.get(i));

            labels.get(i).show();
             initComponents();

        /* labels.add(new JLabel("_"));
        this.add(labels.get(i));
        //labels[i].setText("_");
        System.out.println(labels.get(i));
        * */


    }
}
    }

but i got null pointer exception, i don’t know why?

Someone can help me or show what wrong in my code.

Thank you!

  • 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-30T16:57:04+00:00Added an answer on May 30, 2026 at 4:57 pm

    Can’t see anything obvious, so I’d start by refactoring to

    for(int i =0; i< chars.length ;i++){
            JLabel l = new JLabel("_");
            labels.add(l);
            this.add(l);
            //labels[i].setText("_");
            System.out.println(l);
        }
    

    which may help you to see the problem. It also avoids repeating the get(i) [probably not expensive on an ArrayList, but still]

    BTW: what type is ‘this’?

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

Sidebar

Related Questions

I try to create a client server socket beetwen my droid(client) and my PC(server),
When creating a Socket in Java: new Socket(host, port); The Socket constructor will try
i have written simple client server socket programming code as follow MyServer.java import java.io.DataInputStream;
I have created a sample java socket application. I used Socket s = new
Eclipse and MyEclipse create new Java files with an extra blank line after the
I am trying to make a simple Client-Server application but when I execute the
I've a server in Java which listens the request on the UDP socket port
I hate starting a post with this but I'm new to Java... I've followed
I'm trying to make a simple http client server using java. It will show
Possible Duplicate: Java socket sends some data during connection to server I have two

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.