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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:40:17+00:00 2026-06-05T04:40:17+00:00

The below program is used to take the input of integers from a text

  • 0

The below program is used to take the input of integers from a text file and plot a XY graph.I have used JFileChooser to select the file. I’m trying to get the complete path of the file. I have created an ActionListener method for the button. When I run it I get the following message:

Exception in thread "main" java.lang.NullPointerException
at java.io.File.<init>(Unknown Source)
at pia.main(pia.java:34)
import java.awt.event.ActionEvent;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.xy.*;

public class Pio {

    public static void main(String[] args) throws IOException {
        JButton but = new JButton("Open file");
        String URL = null;
        but.addActionListener(new ActionListerner()                         

    {

            public void actionPerformed(ActionEvent e) {

           JFileChooser chooser = new JFileChooser();

            int ret = chooser.showDialog(null, "Open file");

            if (ret == JFileChooser.APPROVE_OPTION) {

              File file = chooser.getSelectedFile();

             String URL = file.getPath();

          }

     });

        File f = new File(URL);
        FileReader inputF = new FileReader(f);
        BufferedReader in = new BufferedReader(inputF);

        int[] a = new int[100];
        int[] b = new int[100];
        String s = in.readLine();

        int i = 0;

        while (s != null && i < 100) { // your arrays can only hold 1000 ints
            String pair[] = s.split(" ");

            a[i] = Integer.parseInt(pair[0]);
            b[i] = Integer.parseInt(pair[1]);
            i++; // don't forget to increment

            s = in.readLine();
        }

        in.close();
        System.out.println("the output of the file is " + f);

        XYSeries data = new XYSeries("Line Graph");
        int n = a.length;

        for (int j = 0; j < n; j++) {
            data.add(a[j], b[j]);
        }

        XYDataset xY = new XYSeriesCollection(data);
        JFreeChart chart = ChartFactory.createXYLineChart("line graph", "Cycles", "Temperature", xY, PlotOrientation.VERTICAL, true, true, true);
        ChartFrame frame = new ChartFrame("Example", chart);

        JPanel panel = new JPanel();
        panel.add(but);
        frame.add(panel);
        frame.setSize(1000, 500);
        frame.setVisible(true);
    }
}
  • 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-05T04:40:19+00:00Added an answer on June 5, 2026 at 4:40 am

    Define a class field so that the scope will be across the entire class, something like:

    private String filePath;
    
    // ...
    
    public void actionPerformed(ActionEvent e)
    {
        JFileChooser chooser=new JFileChooser();
        int ret = chooser.showDialog(null, "Open file");
        if(ret == JFileChooser.APPROVE_OPTION)
        {
            File file = chooser.getSelectedFile();
            filePath = file.getPath();
        }
    }
    

    Now, you can use it like this:

    if(filePath != null) System.out.println("The file path: " + filePath);
    else System.out.println("The file path is not set yet!");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run a shell from my python program. I have used
I'm trying to take 9x9, 12x12, 15x15, etc. arrays and have the program interpret
in the below program i have used static const int init. But it is
In the below program, sometime i get following output: Number Format Execption For input
I have a Enumeration as shown in below program public class Test { public
(In short: main()'s WaitForSingleObject hangs in the program below). I'm trying to write a
I have a situation where I need to extract dates from the file names
The c++ program below fails to read the file. I know using cstdio is
In the program below, I am trying to calculate the distance between two points.
For a while now the installer for my program has used the below code

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.