HSSFCell读取空错误?

时间:2017-09-20 11:56:31

标签: java json excel apache-poi

我一直在尝试为志愿者项目编写一个Excel-to-JSON转换器,但我一直遇到无法纠正的无法纠正的错误。这是我的错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at bin.parse.MembersParser.tryString(MembersParser.java:112)
at bin.parse.MembersParser.parseFile(MembersParser.java:53)
at bin.ParserFrame.actionPerformed(ParserFrame.java:61)
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)

我不能让它消失,不管我做了多少改变,所以这里有来源,也许别人可以告诉我我做错了什么。

bin.Main:

package bin;

import javax.swing.JFrame;

public class Main {
    static ParserFrame f;

    public static void main(String[] args) {
        f = new ParserFrame();
        f.add(f.panel);
        f.setSize(800, 200);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

}

bin.ParserFrame:

    package bin;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

import javax.swing.*;

import bin.parse.*;

@SuppressWarnings("serial")
public class ParserFrame extends JFrame implements ActionListener {
    private JButton parse;
    private JLabel desc, inpt, oupt;
    private JTextField input, output;
    final JComboBox<String> menu;
    final int FIELDS_WIDTHS = 35;
    String[] CHOICES = {"Members", "MembersDues", "MembersLevel", "MembersAddresses", "MembersContact"};

    private Parser p;

    public JPanel panel = new JPanel();

    public ParserFrame() {
        desc = new JLabel("Select type of format to parse: ");
        menu = new JComboBox<String>(CHOICES);
        menu.setVisible(true);
        inpt = new JLabel("Select input source .xls file: ");
        input = new JTextField(FIELDS_WIDTHS);
        oupt = new JLabel("Select output location (end with .txt extension): ");
        output = new JTextField(FIELDS_WIDTHS);
        parse = new JButton("Parse the file.");
        parse.addActionListener(this);

        panel.setLayout(new GridLayout(4, 2));
        panel.add(desc);
        panel.add(menu);
        panel.add(inpt);
        panel.add(input);
        panel.add(oupt);
        panel.add(output);
        panel.add(parse);
    }

    private void createButton() {
        parse = new JButton("Parse File");
        parse.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        p = selectParser((String) menu.getSelectedItem());
        String outputJSON = p.parseFile(input.getText());
        try(PrintWriter out = new PrintWriter(output.getText())){
            out.println(outputJSON);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
    }

    private Parser selectParser(String selection) {
        switch (selection) {
        case "Members": return new MembersParser();
        }
        return null;
    }
}

bin.parse.MembersParser:

package bin.parse;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import bin.format.Members;

public class MembersParser implements Parser {
    Gson g;
    List<Members> j = new ArrayList<Members>();
    Members m = new Members();
    HSSFWorkbook w = null;
    HSSFRow r = null;
    HSSFCell c = null;

    public MembersParser() {
        g = new GsonBuilder().setPrettyPrinting().setDateFormat("yyyy-MM-dd'T'HH:mm:ssz").create();
    }

    public String parseFile(String inputLocation) {
        try {
            w = Parser.readFile(inputLocation);
        } catch (IOException e) {
            e.printStackTrace();
        }

        HSSFSheet s = w.getSheetAt(0);
        for(int i = 1; i < s.getPhysicalNumberOfRows(); i++) {
            r = s.getRow(i);

            c = r.getCell(0);
            m.id = tryNumber(c);
            c = r.getCell(1);
            m.firstName = tryString(c);
            c = r.getCell(2);
            m.lastName = tryString(c);
            c = r.getCell(3);
            m.title = tryString(c);
            c = r.getCell(4);
            m.suffix = tryString(c);
            c = r.getCell(5);
            m.nickname = tryString(c);
            c = r.getCell(6);
            m.fullName = tryString(c);
            c = r.getCell(7);
            m.chapterName = tryString(c);
            c = r.getCell(8);
            m.gender = tryString(c);
            c = r.getCell(9);
            m.ageGroup = tryString(c);
            c = r.getCell(10);
            m.unit = tryNumber(c);
            c = r.getCell(11);
            m.level = tryString(c);
            c = r.getCell(12);
            m.levelDate = getDate(c);
            c = r.getCell(13);
            m.currentPosition = tryString(c);
            c = r.getCell(14);
            m.youthProtectionDate = getDate(c);

            j.add(m);
        }

        return g.toJson(m);
    }

    private String getString(HSSFCell c) {
        try {
            return c.getStringCellValue();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }

        return "";
    }

    private int getNumber(HSSFCell c) {
        try {
            return (int) c.getNumericCellValue();
        } catch (NullPointerException e) {
            e.printStackTrace();
        } 

        return 0;
    }

    private Date getDate(HSSFCell c) {
        try {
            return c.getDateCellValue();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }

        return null;
    }

    private String tryString(HSSFCell c) {
        if(c.getCellTypeEnum() == CellType.STRING) {
            return getString(c);
        } else if (c.getCellTypeEnum() == CellType.NUMERIC) {
            return String.valueOf(getNumber(c));
        } else {
            return "";
        }
    }

    private int tryNumber(HSSFCell c) {
        if(c.getCellTypeEnum() == CellType.NUMERIC) {
            return getNumber(c);
        } else if (c.getCellTypeEnum() == CellType.STRING) {
            return Integer.valueOf(getString(c));
        } else {
            return 0;
        }
    }
}

注意:除了readFile()

之外,解析器父级目前没有任何功能。

bin.format.Members:

package bin.format;

import java.util.Date;

/**
 * Holds the data from the Excel sheet and formats it for the JSON output.
 * 
 * @version 1
 */

public class Members {
    public int id;
    public String firstName;
    public String lastName;
    public String title;
    public String suffix;
    public String nickname;
    public String fullName;
    public String chapterName;
    public String gender;
    public String ageGroup;
    public int unit;
    public String level;
    public Date levelDate;
    public String currentPosition;
    public Date youthProtectionDate;
}

1 个答案:

答案 0 :(得分:2)

你的意思是你所有的行都被填满了(从0到15);但在某些情况下,情况似乎并非如此。

Nullpointer出现在tryString()中,所以我假设在某些情况下c为空。

如果您只是添加

if(c == null) {
  return "";
}

在tryString的开头(和其他tryXXX方法类似),这应该没问题。