Results 1 to 5 of 5
  1. #1
    Andros is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default Help with TextFileInput

    Hello, I'm new to the forums and to Java Programming. I have some questions about programs I'm writing with TextFileInput I have 5 programs to write by Monday so hopefully you guys can help me out :).

    I finished writing the first program. Here it is:

    import javax.swing.*;
    import java.lang.*;
    public class Lab5Program1 {

    public static int inputFromFile (String filename)
    {
    TextFileInput in = new TextFileInput (filename);
    int lengthfilled = 0;
    String line = in.readLine();
    while(line!=null) {
    wordArray[lengthfilled] = line;
    lengthfilled ++;
    line = in.readLine();
    }
    in.close();
    return lengthfilled;
    }

    public static String isOrIsNot, inputWord;
    public static String wordArray [];

    public static void main(String[] args) {
    inputFromFile ("Lab5Input.txt");
    while(true)
    {
    inputWord = JOptionPane.showInputDialog(null,"Enter a word in all lower case:");

    if(inputWord.equals("Stop")) System.exit(0);
    if (wordIsThere(inputWord,wordArray))
    isOrIsNot = "is";
    else
    isOrIsNot = "is not";
    JOptionPane.showMessageDialog(null,"The word "+inputWord+" "+
    isOrIsNot+" on the list.");
    }
    }

    public static boolean wordIsThere(String findMe, String[] theList){
    for(int i=0;i<theList.length;i++){
    if(findMe.equals(theList[i])) return true;
    }
    return false;
    }
    }
    However, when I compile it tells me that I have a "Null Point Exception". The problem lines (according to the compiler) are indicated in bold. Can anyone tell me how to fix it?

  2. #2
    Andros is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default

    Anyone? I've been such on this for a long time and I still have four programs to go.

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    You need to initialize wordArray, i.e.

    Java Code:
    public static String[] wordArray = new String[NUMWORDSNEEDED];
    You might be better of with an ArrayList<String> though... you don't seem to know exactly how many lines there are, as far as I can tell.
    Last edited by Singing Boyo; 03-07-2010 at 05:07 AM. Reason: forgot the slash in the closing code tag
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    nigamsir is offline Member
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

    Default

    instead of .dat u make it .txt

    import java.io.DataInputStream;
    04.import java.io.FileInputStream;
    05.import java.io.DataOutputStream;
    06.import java.io.FileOutputStream;
    07.import java.io.IOException;
    08.
    09.public class PrimitiveStreamExample {
    10.
    11. public static void main(String[] args) {
    12. //
    13. // Prepares some data to be written to a file.
    14. //
    15. int cityIdA = 1;
    16. String cityNameA = "Green Lake City";
    17. int cityPopulationA = 500000;
    18. float cityTempA = 15.50f;
    19.
    20. int cityIdB = 2;
    21. String cityNameB = "Salt Lake City";
    22. int cityPopulationB = 250000;
    23. float cityTempB = 10.45f;
    24.
    25. try {
    26. //
    27. // Create an instance of FileOutputStream with cities.dat as the
    28. // file name to be created. Then we pass the input stream object
    29. // in the DataOutputStream constructor.
    30. //
    31. FileOutputStream fos = new FileOutputStream("cities.dat");
    32. DataOutputStream dos = new DataOutputStream(fos);
    33.
    34. //
    35. // Below we write some data to the cities.dat. DataOutputStream class
    36. // have various method that allow us to write primitive type data and
    37. // string. There are method called writeInt(), writeFloat(),
    38. // writeUTF(), etc.
    39. //
    40. dos.writeInt(cityIdA);
    41. dos.writeUTF(cityNameA);
    42. dos.writeInt(cityPopulationA);
    43. dos.writeFloat(cityTempA);
    44.
    45. dos.writeInt(cityIdB);
    46. dos.writeUTF(cityNameB);
    47. dos.writeInt(cityPopulationB);
    48. dos.writeFloat(cityTempB);
    49.
    50. dos.flush();
    51. dos.close();
    52.
    53. //
    54. // Now we have a cities.dat file with some data in it. Next you'll
    55. // see how easily we can read back this data and display it. Just
    56. // like the DataOutputStream the DataInputStream class have the
    57. // corresponding read methods to read data from the file. Some of
    58. // the method names are readInt(), readFloat(), readUTF(), etc.
    59. //
    60. FileInputStream fis = new FileInputStream("cities.dat");
    61. DataInputStream dis = new DataInputStream(fis);
    62.
    63. int cityId = dis.readInt();
    64. System.out.println("City Id: " + cityId);
    65. String cityName = dis.readUTF();
    66. System.out.println("City Name: " + cityName);
    67. int cityPopulation = dis.readInt();
    68. System.out.println("City Population: " + cityPopulation);
    69. float cityTemperature = dis.readFloat();
    70. System.out.println("City Temperature: " + cityTemperature);
    71. } catch (IOException e) {
    72. e.printStackTrace();
    73. }
    74. }
    75.}

    source: Java Examples - How do I use DataInputStream and DataOutputStream?

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nigamsir View Post
    How does that all apply to the NullPointerException mentioned by the OP?

    kind regards,

    Jos

Posting Permissions

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