1

I hope my question is understandable, and I hope this section is the right one. I have to build a converter from Apache log file format to IIS one, and I built a system which works from command line. I just added a GUI and for the first time I used a JFileChooser. The problem started there: I get a strange error. This is the code which generates the error:

public class HTTPiis extends JPanel{

public HTTPiis() {
    setLayout(new FlowLayout());

    JButton selStart = new JButton("Apri");
    JButton selDest = new JButton("Seleziona destinazione");
    JButton converti = new JButton("Converti");

    add(selStart);
    add(selDest);
    add(converti);

    selStart.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            final JFileChooser chooser = new JFileChooser();
            chooser.showOpenDialog(new JPanel());

            String source = chooser.getSelectedFile().getAbsolutePath();
            FileHandler fh = new HTTPtoIIS();
            fh.setFilePath(source);
            HTTPtoIIS h = new HTTPtoIIS();
            h.convert();
        }
    });
}

} and this is the error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.io.File.<init>(File.java:277)
at tesi.FileLoader.load(FileLoader.java:17)
at tesi.HTTPLogHandler.parse(HTTPLogHandler.java:17)
at tesi.HTTPtoIIS.convert(HTTPtoIIS.java:49)
at grafica.HTTPiis$1.actionPerformed(HTTPiis.java:41)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Can you kindly explain me why I'm wrong and where? If you need more code just ask me! Thankyou!!

lucad93
  • 45
  • 6
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – ΦXocę 웃 Пepeúpa ツ Nov 15 '16 at 15:04
  • in which line of the code you posted occurs the error? try removing final from the filechooser, i bet that will fix it – XtremeBaumer Nov 15 '16 at 15:05
  • Well, the error occurs in the line where there's `h.convert`. I tried to remove final but I get the error in the same way :( – lucad93 Nov 15 '16 at 15:15
  • Can you debug to see what the value of `source` is after you select it? – Dan W Nov 15 '16 at 15:17
  • I've done, i get the right value. – lucad93 Nov 15 '16 at 15:18
  • The problem is not JFileChooser, it is most likely the convert() method. Can you please edit your question to include it? – Viorel Florian Nov 15 '16 at 15:20
  • This code: ` FileHandler fh = new HTTPtoIIS(); fh.setFilePath(source); HTTPtoIIS h = new HTTPtoIIS(); h.convert();`, is creating two instances of HTTPtoIIS(). Then it is setting the user-selected file to on, but using the other to call convert(). I do not know how your class works, but it seems the second instance does not have the path thus failing on the NPE. Isn't that the problem? – david a. Nov 15 '16 at 15:21
  • I adjusted that, but the code seems to get the same error... – lucad93 Nov 15 '16 at 15:55

2 Answers2

2

Why are you adding an empty panel to the file chooser?

To me the error looks like the problem is with your converter code. So did you:

  1. do any basic debugging?

  2. display the String value returned from the file chooser?

  3. try to hardcode the String value to determine if the problem is with the String or the converter code?

Until you can provide us with more details we can't suggest a specific solution.

Maybe your code needs a fully qualified file name. Maybe it needs a relative name. We don't know because you wrote the converter code. All a file chooser does is give you a file name. It is up to you to get the proper format of the file name.

Read the section from the Swing tutorial on How to Use File Choosers for working examples.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1.Yes, and I found the error where I said; 2. Yes, and I get the right value; 3. What do you mean with "hardcode the String"? – lucad93 Nov 15 '16 at 15:28
  • @lucad93. The file chooser is irrelevant to the problem. The problem is in the conversion code that you wrote. So this means that you are passing invalid data to that particular method. So first you hardcode the data you pass to the method and get your conversion code working. So maybe the hard coded value is something like "c:/somedirectory/logfilename.txt". Then once you know the exact format of the file name you pass to the method you use the file chooser to get the name dynamically and make sure if it formatted correctly. – camickr Nov 15 '16 at 15:36
  • Ok, I started from there. If I put the path right into the code it works, i add the file chooser and stops to work. – lucad93 Nov 15 '16 at 15:40
  • @lucad93, exactly, so that means the data you get from the file chooser is not formatted the way you want. So you need to get the file name from the file chooser and make sure it is formatted correctly. We don't know what your format is so it is up to you to read the `File` API for the different methods that allow you to get the file name in the format you need. Or you need to change your conversion code to accept a different file name format. – camickr Nov 15 '16 at 15:45
  • I analized the software and I noticed that when I pass the filePath from the source code It works, but when I pass filePath from the test class It crashes... I'm just wondering why! – lucad93 Nov 16 '16 at 09:40
  • @lucad93, The software should be irrelevant. What is important is the data you pass to the software. If you pass the same data you should get the same results. Your conversion code doesn't know or care where the String comes from. It only cares that it is a proper format. So you need to format the String from the file chooser to be in a format that your conversion routine expects. How you do this is up to you. I already you look at the File API to help you in the formatting of the file name. – camickr Nov 16 '16 at 15:57
1

So this is an issue on how to debug your code. It is good that you posted the stack trace, because that is where you begin investigating/debugging your issue.

In simple terms, a stack trace is a list of the method calls that the application was in the middle of when an Exception was thrown.

For more information on that What is a stack trace, and how can I use it to debug my application errors?

The list of method calls is sorted, meaning that the one on top is the most recent call (closest to the exception) and the one at the bottom is the one that started this process. Try to interpret it like this: enter image description here

So the first step is to look at th code at at tesi.FileLoader.load(FileLoader.java:17)

Community
  • 1
  • 1
Viorel Florian
  • 594
  • 9
  • 29