-1

Below is the code I'm working on. The code will execute a Swing-based GUI, with a simple JFrame that holds a button which when pressed will run the Ping utility in the background. If the host is either reachable or not, a dialog will be displayed with a successful/unsuccess message.

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.InetAddress;

public class App {

private JTextArea clickThisButtonTextArea;
public JButton button1;
private JPanel panelMain;

public App() {
    button1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            try {
                sendPing("8.8.8.8");
                //JOptionPane.showMessageDialog(null, sendPing("192.168.0.1"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
}

public String sendPing(String ipAddr) throws IOException {
    InetAddress ip = InetAddress.getByName(ipAddr);
    boolean ipReach = ip.isReachable(5000);
    System.out.println("Sending Ping Request to " + ipAddr);
    if (ip.isReachable(5000)) {
        JOptionPane.showMessageDialog(null, "Host is reachable!");
        System.out.println("Host is reachable");
    } else {
        JOptionPane.showMessageDialog(null, "Sorry, no host!");
        System.out.println("Sorry ! We can't reach to this host");
    }
    return null;
}

public static void main(String[] args) {
    JFrame frame = new JFrame("App");
    frame.setContentPane(new App().panelMain);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    frame.setSize(250,250);


}

}

The image below shows a tcpdump of traffic flowing out after the button is pressed vs. a ping ran from the terminal. enter image description here

Any insights will be greatly appreciated.

95C3ntS
  • 9
  • 7
  • I am not sure what your question is. Do you nede help showing the dialogue? Do you need help waiting before you show the dialogue? Do you nede help putting the information in the dialogue? – sorifiend Sep 01 '20 at 02:43
  • So, apart from you code probably generating a `NullPointerException` and/or not showing the button ... what's your question? – MadProgrammer Sep 01 '20 at 02:46
  • Ah! sorry, should've been more explicit explaining basic networking. When I ping any internal/private IP (i.e. 192.168.x.x) it runs fine. But when I run any public IP (i.e. trying to reach the internet, the ping won't go through. I could go into the details on how a ping works, but I don't wanna bore you. Thanks! – 95C3ntS Sep 06 '20 at 05:29

1 Answers1

2

So, running your code generates

Exception in thread "main" java.lang.NullPointerException
    at sotest.App.<init>(App.java:25)
    at sotest.App.main(App.java:54)

This is because button1 is never initialised. I can fix it by updating the constructor like so:

public App() {

    button1 = new JButton("Ping");        
    button1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            try {
                sendPing("8.8.8.8");
                //JOptionPane.showMessageDialog(null, sendPing("192.168.0.1"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
}

But now when I run it, I get...

Exception in thread "main" java.awt.IllegalComponentStateException: contentPane cannot be set to null.
    at java.desktop/javax.swing.JRootPane.setContentPane(JRootPane.java:598)
    at java.desktop/javax.swing.JFrame.setContentPane(JFrame.java:679)
    at sotest.App.main(App.java:56)

This is because panelMain has never been initialised. I can fix it by updating the constructor like so:

public App() {

    panelMain = new JPanel();
    
    button1 = new JButton("Ping");
    button1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            try {
                sendPing("8.8.8.8");
                //JOptionPane.showMessageDialog(null, sendPing("192.168.0.1"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
}

Ah, but now when I run it, it's not displaying anything. This is because I've not added the button to the panel, once again, we update the constructor like so:

public App() {

    panelMain = new JPanel();
    
    button1 = new JButton("Ping");
    button1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            try {
                sendPing("8.8.8.8");
                //JOptionPane.showMessageDialog(null, sendPing("192.168.0.1"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    
    panelMain.add(button1);
}

All of this is pretty basic Java and would suggest you might need to spend some more time reading through Creating a GUI With JFC/Swing.

Your next problem is going to more tricky to solve, but you can get a start with Concurrency in Swing

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Ah! sorry, should've been more explicit explaining basic networking. When I ping any internal/private IP (i.e. 192.168.x.x) it runs fine. But when I run any public IP (i.e. trying to reach the internet, the ping won't go through. I could go into the details on how a ping works, but I don't wanna bore you. Thanks! – 95C3ntS Sep 06 '20 at 05:29
  • @95C3ntS Well, I'd argue that that is beyond the immediate issues of the NPE and the `IllegalComponentStateException` and starts to feature into the realms of another question – MadProgrammer Sep 06 '20 at 06:13