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

The Archive Base Latest Questions

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

I try to read all lines in the text area into StringBuilder so that

  • 0

I try to read all lines in the text area into StringBuilder so that I can use the text in text area as a whole string. However I get the NullPointerException in line s.append(line);. Why?

public class tu extends JFrame implements ActionListener{

    JTextArea t;
    static StringBuilder s;

    tu (){

        setLayout(/*new BorderLayout()*/null);
        t=new JTextArea(30,77);
        JScrollPane s=new JScrollPane(t,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        JButton b=new JButton("process");
        b.addActionListener(this);
        JPanel p=new JPanel();
        JPanel p1=new JPanel();
        p.add(s);
        p.setBorder(new TitledBorder("sequence"));
        p.setBounds(0,0,880,540);
        p1.add(b);
        p1.setBounds(380,570,100,40);
        add(p);
        add(p1);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(900,700);
        setVisible(true);

    }
     public void actionPerformed(ActionEvent e){
         String text=t.getText();
         while (text!=null){for (String line : text.split("\\n")){ 
         s.append(line);
         }}

     }


    public static void main(String[] args){
        tu t=new tu();

    }
}
  • 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-18T12:51:08+00:00Added an answer on June 18, 2026 at 12:51 pm

    Attempting to invoke any method on an object that has not been instantiated will result in an NPE. You need to instantiate your StringBuilder s:

    StringBuilder s = new StringBuilder();
    

    Do you really want to wait until the user has clicked the JButton before building up the contents of this StringBuilder? StringBuilders are typically used as on-demand helper objects with local scope.


    A number of things to note:

    • Use of static member variables is considered poor design
    • Class names start with uppercase letters
    • Don’t use absolute positioning(null layout). Use a layout manager instead
    • The preferred approach is to create an instance of JFrame directly & use.
    • The preferred approach for ActionListeners is to use either an anonymous instance or the Action interface.
    • Consider using initial threads
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to read a text files with 20x20 data into variable C, and
When I try to read bash history into vim, I get nothing. :r !history
Is it possible to use streamreader to find all the lines that match tags
I try to read out all existing calendars. I want have a HashMap with
I try to read out all existing calendars. I tried the example from here:
i try to read Data from an Oracle-Database. The problem is that in some
When i try to read a JSON string like below it goes to endless
As a relative newbie I try to read as much as I can about
I see that when I try to read the value from a textarea field
I get a warning in MSVC++ when I try to read an integer from

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.