Results 1 to 2 of 2
  1. #1
    cheskers11 is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Default NullPointerException in FileInputStream text file

    package Cheska;
    import java.io.*;

    public class LongExam {

    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws IOException {

    String thisLine = "", thisLine1 = "", firstParse = "", secondParse = "";
    int total = 0, total1 = 0, totalAmt = 0;

    FileOutputStream fos = new FileOutputStream ("src/Cheska/totals.txt");
    PrintStream myOutput = new PrintStream(fos);

    FileInputStream fis = new FileInputStream("src/Cheska/products.txt");
    DataInputStream myInput = new DataInputStream(fis);

    FileInputStream fis1 = new FileInputStream("src/Cheska/price.txt");
    DataInputStream myInput1 = new DataInputStream(fis1);
    thisLine = myInput.readLine();
    thisLine1 = myInput1.readLine();
    System.out.println(thisLine1.substring(19,21));

    myOutput.println("Welcome to INPROLA Grocery!");
    myOutput.println(" ");
    myOutput.println(" ");
    myOutput.println(" Item Qty");

    while (thisLine != null)
    {
    firstParse = thisLine.substring(19,21);
    total = total + Integer.parseInt(firstParse);
    secondParse = thisLine1.substring(19,21);
    total1 = Integer.parseInt(secondParse);
    totalAmt = total1 * total;
    myOutput.println(thisLine + " " + totalAmt);
    thisLine=myInput.readLine();
    thisLine1 = myInput.readLine();
    }


    {
    myOutput.println("----------------------------");
    myOutput.println("The total: " + total + " " + totalAmt);
    myOutput.println(" ");
    myOutput.println(" ");
    myOutput.println("Thank You for Shopping! ^_^ Have a nice day!");
    }
    }
    }



    Hi, I'm a first year IT student. I'm new to java. I can't seem to get the error from this while loop.

    while (thisLine != null)
    {
    firstParse = thisLine.substring(19,21);
    total = total + Integer.parseInt(firstParse);
    secondParse = thisLine1.substring(19,21);
    total1 = Integer.parseInt(secondParse);
    totalAmt = total1 * total;
    myOutput.println(thisLine + " " + totalAmt);
    thisLine=myInput.readLine();
    thisLine1 = myInput.readLine();
    }

    The bold line is said to be null, but I've double-checked the codes, I can't seem to see anything wrong with it. Help?

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    1.) Please use code tags when posting code.
    2.) Use FileReader with BufferedReader to read character files. Those classes you are using are used for reading binary data e.g .exe files.

Similar Threads

  1. Replies: 8
    Last Post: 11-06-2009, 06:52 AM
  2. Replies: 13
    Last Post: 09-16-2009, 01:05 PM
  3. Replies: 0
    Last Post: 12-01-2008, 09:50 PM
  4. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:04 PM
  5. Replies: 6
    Last Post: 05-14-2008, 04:04 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •