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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:29:07+00:00 2026-06-03T05:29:07+00:00

I have a portion of code that takes the text of a TextField (there

  • 0

I have a portion of code that takes the text of a TextField (there are two TextFields actually, but the user can use only one at a time) and search it into a file stored into the terminal. The problem is that I always get a null string, even when there is text into one of the TextFields…
This is the code (the assignation is on the method actionPerformed():

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Search implements ActionListener{
JFrame frame;
JButton click;
JLabel comando, carico;
JTextField textv, text;
JTextArea res;
String pathFile = "C:\\Log.txt";
String str= new String();

Search(){

    frame = new JFrame("Search");
    frame.setSize(400, 200);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(1,2));
    frame.setResizable(false);
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(7,1));
    click = new JButton("Cerca");
    comando = new JLabel("Comando");
    carico = new JLabel("A carico di:");
    textv = new JTextField("");
    text = new JTextField("");
    res = new JTextArea("");
    panel.add(comando);
    panel.add(textv);
    panel.add(carico);
    panel.add(text);
    panel.add(click);
    res.setLineWrap(true);
    res.setWrapStyleWord(true);
    res.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));

    JScrollPane scroller = new JScrollPane(res);
    scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    panel.add(scroller);
    frame.add(panel);
    click.addActionListener(this);      
    click.setSize(70, 35);      
    frame.setVisible(true);

}

public void actionPerformed (ActionEvent e){
    if(e.getSource()==click){
        res.setText(null);
        if(textv != null) {cercaStringa(pathFile, textv.getText().toString());}
        else {cercaStringa(pathFile, text.getText().toString());}
    }
}

public void cercaStringa(String pathFile, String stringa){
    try {
        BufferedReader in = new BufferedReader(new FileReader(pathFile));
        String line = new String();
        while((line = in.readLine())!=null) {   
            if(line.contains(stringa)){
                res.append(line);
                res.append("\n");
                }
        }
    }
    catch(IOException ex) { ex.printStackTrace();}
    }



public static void main (String[] args){
    new Search();
}

}

I’m really going to throw everything outside the window cause I know the solution is simple but I can’t get it…

  • 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-03T05:29:08+00:00Added an answer on June 3, 2026 at 5:29 am

    As confirmed by the following test, your fields are not null as of the actionListener call. For example, input of "Comando" and "Carico" outputs "Test: Comando" followed by "Test: Carico", as expected:

    @Override
    public void actionPerformed(ActionEvent e) {
      if (e.getSource() == click) {
        res.setText(null);
        System.out.println("Test: "+textv.getText());
        System.out.println("Test: "+text.getText());
        if (textv != null) {
          cercaStringa(pathFile, textv.getText().toString());
        } else {
          cercaStringa(pathFile, text.getText().toString());
        }
      }
    }
    

    The problem is that if (textv != null) will never reach the “else” at the first run (as far as I can tell in the quick-look I gave the code, it never will on any run, as textv is never set null), because textv = new JTextField(""); is not equivalent to textv = null, and textv will still be holding a String, albeit it’s an empty one.

    The fix is this:

    public void actionPerformed(ActionEvent e) {
      if (e.getSource() == click) {
        res.setText("");
        if (!textv.getText().isEmpty()) {
          cercaStringa(pathFile, textv.getText().toString());
        } else {
          cercaStringa(pathFile, text.getText().toString());
        }
      }
    }
    

    You were also missing a verification for the existence of C:\Log.txt, and an exception could be easily triggered.

    Here is the quick-fix, in the Main:

    public static void main(String[] args) {
      File log = new File("C:\\Log.txt");
      if (!log.exists()) {
        try {
          /*
           * mkdirs() is not really needed when using the C root,
           * but I like to do this because the path might be changed.
           */
          if (!log.getParentFile().exists()) {
            log.getParentFile().mkdirs();
          }
          log.createNewFile();
        } catch (IOException ex) {
          Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
      Search search = new Search();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about technology or methodology out there that I can use
I have a method that takes several params, I need to delay a portion
Ok, so I have a form that takes a username and a code. This
I have two snippets of code that tries to convert a float list to
Hi all i have some code below that allows me to print the text
With ASP .NET MVC3. In my controller I have this portion of code MasterMindDnetEntities
Let's say we have a code portion like this: IProduct product = ProductCreator.CreateProduct(); //Factory
The problem: we have one application that has a portion which is used by
I have a script we use. What I need a portion of this to
I am building a site that will (obvisouly) have a front end public portion

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.