-1

When I try to create a loop that will put all items in an array into a JList, I get a problem in which a NullPointerException occurs, and I have NO idea what's going on.

*I don't know exactly how to word this, I'm just very confused

Here is the part I am having trouble on:

String[] files = null;

try {
//Something here
    files = client.listNames();
    for(String item: files){
        model.addElement(item);
    }

JList list = new JList(model);
list.setSelectionMode(0);
JScrollPane scroll = new JScrollPane(list);
scroll.setBounds(110, 10, 560, 630);
pane.add(scroll);

}catch (IOException e) {
    e.printStackTrace();
}

If you need some more code, here is the full program: package p1;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.SocketException;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

public class OperaFTP{

private static FTPClient client = new FTPClient();
private static OperaFTP Operation = new OperaFTP();
private static JLabel Cr;
JFrame loginWindow = new JFrame("Login");

public static void main(String[] args){
    Operation.firstWindow();
}

private void firstWindow(){

     JPanel loginPane = new JPanel(null);

     JLabel title;
     JLabel ftpMore;
     JLabel userName;
     JLabel passWord;

     JButton connect = new JButton("Connect");

    loginWindow.setDefaultCloseOperation(3);
    loginWindow.setSize(300, 340);
    loginWindow.setResizable(false);
    loginWindow.setLocationRelativeTo(null);

    loginWindow.add(loginPane);

    title = new JLabel("FTP LOGIN");
    title.setFont(new Font("Times New Roman", 1, 40));
    title.setForeground(Color.RED);
    title.setBounds(35, 6, 300, 60);
    loginPane.add(title);

    ftpMore = new JLabel("ftp://");
    ftpMore.setBounds(20, 76, 60, 30);
    ftpMore.setForeground(Color.BLUE);
    ftpMore.setFont(new Font("Times New Roman", 0, 16));
    loginPane.add(ftpMore);

    JTextField ftpIp = new JTextField();
    ftpIp.setBounds(50, 80, 190, 25);
    loginPane.add(ftpIp);

    userName = new JLabel("Username:");
    userName.setBounds(70, 115, 60, 30);
    userName.setFont(new Font("Times New Roman", 0, 14));
    loginPane.add(userName);

    JTextField userField = new JTextField();
    userField.setBounds(70, 140, 150, 25);
    loginPane.add(userField);

    passWord = new JLabel("Password:");
    passWord.setBounds(70, 175, 60, 30);
    passWord.setFont(new Font("Times New Roman", 0, 14));
    loginPane.add(passWord);

    JPasswordField passField = new JPasswordField();
    passField.setBounds(70, 200, 150, 25);
    loginPane.add(passField);

    connect.setBounds(75, 240, 140, 40);
    connect.setFont(new Font("Arial", 2, 18));
    loginPane.add(connect);

    Cr = new JLabel("Save credentials");
    Cr.setBounds(95, 280, 200, 30);
    loginPane.add(Cr);

    JCheckBox saveCr = new JCheckBox();
    saveCr.setBounds(75, 285, 20, 20);
    loginPane.add(saveCr);

    File ftpC = new File(System.getProperty("user.home") + "\\Documents\\FTPFILECREDIANTIALS.txt");

    String xx = null;
    String xx2 = null;
    String xx3 = null;
    if (ftpC.exists()){
      try{
        BufferedReader read = new BufferedReader(new FileReader(ftpC));

        xx = read.readLine();
        xx2 = read.readLine();
        xx3 = read.readLine();

        read.close();
      }
      catch (IOException e1){
        System.out.println("FILE NOT FOUND/CANT READ FROM FILE");
      }
    }

    ftpIp.setText(xx);
    userField.setText(xx2);
    passField.setText(xx3);

    saveCr.addItemListener(new ItemListener(){
      public void itemStateChanged(ItemEvent e){
        try{
          PrintWriter write = new PrintWriter(ftpC);

          write.println(ftpIp.getText());
          write.println(userField.getText());
          write.print(passField.getPassword());

          write.close();
        }
        catch (FileNotFoundException e1){
          System.out.println("FILE NOT FOUND:" + e1);
        }
      }
    });

    connect.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
        String user = null;
        String pass = null;
        String server = null;

        server = ftpIp.getText();
        user = userField.getText();
        char[] passs = passField.getPassword();
        pass = passs.toString();

        try{
            login(server, user, pass);
        } catch (IOException e1) {
            System.out.println(e1);
        }

      }

    });

    loginWindow.setVisible(true);
}

public static void login(String server, String user, String pass) throws SocketException, IOException{
    client.connect(server, 21);
    client.login(user, pass);
    client.enterLocalPassiveMode();
    client.setFileType(FTP.BINARY_FILE_TYPE);

    boolean success = client.isConnected();

    if(success){
        Operation.loginWindow.dispose();
        frame();
    }else{

    }

}

@SuppressWarnings("unchecked")
public static void frame(){

    JFrame window = new JFrame();
    JPanel pane = new JPanel(null);

    DefaultListModel model = new DefaultListModel();

    window.setSize(700, 700);
    window.setResizable(false);
    window.setLocationRelativeTo(null);

    window.add(pane);

    JButton upload = new JButton("Upload");
    upload.setBounds(5, 5, 90, 30);
    upload.setFont(new Font("Arial", 0, 12));
    pane.add(upload);

    JButton download = new JButton("Download");
    download.setBounds(5, 40, 90, 30);
    download.setFont(new Font("Arial", 0, 12));
    pane.add(download);

    JButton delete = new JButton("Delete");
    delete.setBounds(5, 75, 90, 30);
    delete.setFont(new Font("Arial", 0, 12));
    pane.add(delete);

    String[] files = null;

    try {

        files = client.listNames();
        for(String item: files){
            model.addElement(item);
        }

        JList list = new JList(model);
        list.setSelectionMode(0);
        JScrollPane scroll = new JScrollPane(list);
        scroll.setBounds(110, 10, 560, 630);
        pane.add(scroll);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    window.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent windowEvent){
          try{
            client.disconnect();
            client.logout();
          }
          catch (IOException localIOException) {}
          System.exit(1);
        }
      });

    window.setVisible(true);

  }

}

Errors Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at p1.OperaFTP.frame(OperaFTP.java:224) at p1.OperaFTP.login(OperaFTP.java:183) at p1.OperaFTP$2.actionPerformed(OperaFTP.java:161) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

Lezorical
  • 65
  • 1
  • 10

1 Answers1

0

I think your login fails. My IDE shows me this funny wiggly yellow line under a line of code, and warns me: "toString() called on Array instance".

    connect.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
        String user = null;
        String pass = null;
        String server = null;

        server = ftpIp.getText();
        user = userField.getText();
        char[] passs = passField.getPassword();
        pass = passs.toString();               // <----- HERE
System.out.println("pass: " + pass);           // <----- added for debugging
        try{
            login(server, user, pass);
        } catch (IOException e1) {
            System.out.println(e1);
        }
    }

and the System.out.println prints a reference like "pass: [C@36111b04" instead of a password. Change the assignment to pass = new String(passs);

Btw, there's a lot more of these funny wiggly lines. Use of raw types, unused assigned values... and possibly the same issue as above is happening here,

    saveCr.addItemListener(new ItemListener(){
      public void itemStateChanged(ItemEvent e){
        try{
          PrintWriter write = new PrintWriter(ftpC);

          write.println(ftpIp.getText());
          write.println(userField.getText());
          write.print(passField.getPassword());   // <---- HERE

          write.close();
        }
        catch (FileNotFoundException e1){
          System.out.println("FILE NOT FOUND:" + e1);
        }
      }
    });
Arjan
  • 823
  • 1
  • 7
  • 18
  • Okay nice, I was aware if the other stuff, I've made a program like this but lost all code, I just couldn't figure out was happening, ty – Lezorical Jun 14 '16 at 04:30
  • @Lezorical Make your `login` method at least dump some error to the console if it fails, e.g. in `else` block that's empty now, so you can see if it fails while working on your program. – Arjan Jun 14 '16 at 04:49
  • Got it, thx... very helpful :) – Lezorical Jun 14 '16 at 05:08