NullPointerException,java.util.StringTokenizer。<init>(未知来源)</init>

时间:2014-08-04 09:48:54

标签: java

这个程序是在文本文件中添加有关产品的数据,如果第二次给出相同的产品,我会收到一个名为

的异常
Exception in thread "main" java.lang.NullPointerException
    at java.util.StringTokenizer.<init>(Unknown Source)
    at java.util.StringTokenizer.<init>(Unknown Source)
    at ECME.Products.newProd(Products.java:86)
    at ECME.Products.main(Products.java:25)
每当我添加相同移动系统的数据时,都应该生成新的id。      请有人帮帮我。此致

package ECME;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Products implements Product {
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {



        System.out.println("1. Add New Products");
        System.out.println("2. Check Existing Products");
        System.out.println("Please enter your option");
        Products pro = new Products();
        int pOption = sc.nextInt(); 
        switch (pOption) {
        case 1: pro.newProd();

        break;

        case 2:pro.existingProd();

        break;

        default:
            break;
        }
    }

    private int pflag;

    @Override
    public void newProd() {
        System.out.println("Adding Information about new product");
        System.out.println("Enter the product type");
        String pType= sc.next();
        System.out.println("Enter the name of the product");
        String pName= sc.next();
        System.out.println("Enter the cost of the product");
        int pCost = sc.nextInt();
        System.out.println("Enter the number of products you want to add");
        int pQuantity = sc.nextInt();
        int oldPQuantity = pQuantity;

        int num=1;
        try {
            for(int i=1;i<=pQuantity;i++)
            {
                int l=pQuantity;
                File file = new File("D:/ACME/Products.txt");



                if (!file.exists()) {
                    file.createNewFile();
                    throw new IOException();
                }

                FileWriter fileWritter = new FileWriter(file,true);
                @SuppressWarnings("resource")
                BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
                bufferWritter.newLine();


                BufferedReader br1 = null;
                br1 = new BufferedReader(new FileReader("D:/ACME/Products.txt"));

                String entireFileText[]= new String[200];
                int i1 =0;
                while((entireFileText[i1]=br1.readLine())!=null)
                {

                    i1++;
                }   
                //              System.out.println("----<<<"+i1);       
                int j=1;
                StringTokenizer st;
                for(int o=1;o<i;o++){
                    st = new StringTokenizer(entireFileText[o]);

                    String[] im1 = new String[1000];


                    while(st.hasMoreTokens()){
                        im1[j]= (String) st.nextToken();

                        j++;
                    }       


                    //                  System.out.println("-------->"+j);
                    for(int y=1;y<=300;y++)
                    {
                        if(pName.equals(im1[y]))
                        {   int existingQuantity = Integer.parseInt(im1[y+2]);
                        System.out.println("im1[y+2] is-->"+im1[y+2]);
                        oldPQuantity = existingQuantity;
                        pQuantity+=oldPQuantity;
                        System.out.println("opq->"+oldPQuantity);
                        System.out.println("pq->"+pQuantity);
                        pflag=1;
                        break;

                        }
                        if(pflag==1)
                            break;
                    }



                    l++;
                    int op=oldPQuantity+l;
                    //              op++;
                    //              l=op+1;
                    String pId = pName+"-"+num+op;
                    String content = pType+"\t"+pId+"\t"+pName+"\t"+pCost+"\t"+pQuantity;

                    System.out.println(content);
                    bufferWritter.write(content);
                    bufferWritter.close();
                    br1.close();
                }
            } 
        }       
        catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void existingProd() {
        BufferedReader br = null;

        try {

            String sCurrentLine;

            br = new BufferedReader(new FileReader("D:/ACME/Products.txt"));

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }

}

1 个答案:

答案 0 :(得分:1)

entireFileText[o]null。您正在将null值传递给StringTokenizer构造函数