null error problem

Discussion in 'Plugin Development' started by darknesschaos, Feb 12, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    darknesschaos

    I'm not good at all with reading and writing files. My problem is that I always get a null error. How would I fix this?

    Code:
    public void addOps(){
            try{
                // Open the file that is the first
                // command line parameter
                FileInputStream fstream = new FileInputStream("ops.txt");
    
                // Get the object of DataInputStream
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
    
                //Read File Line By Line
                while ((strLine = br.readLine().toLowerCase(Locale.ENGLISH).trim()) != null){
                    plugin.gameOps.add(strLine);
                }
                //Close the input stream
                in.close();
            }
            catch (Exception e){
                System.err.println("Error: ~" + e.getMessage());
            }
        }
     
  2. Offline

    fullwall

    Code:
            File file;
            file = new File("plugins/pluginName", "FileName.ext");
            Scanner scan;
            try {
                if (!file.exists())
                    file.createNewFile();
                scan = new Scanner(file);
                while (scan.hasNextLine()) {
                    String line = scan.nextLine();
                    if (!line.contains("="))
                        continue;
                    if (line.length() == 0)
                        continue;
                    if (line.trim().charAt(0) == '#')
                        continue;
                    int equals = line.indexOf("=");
                    int commentIndex = line.length();
                    String key = line.substring(0, equals).trim();
                    if (key.equals(""))
                        continue;
                    String value = line.substring(equals + 1, commentIndex).trim();
                }
    
            } catch (IOException e) {
                }
    
    This is my file reading code, don't know if it helps.
     
  3. Offline

    darknesschaos

    the problem is I get a null error even though my code checks to prevent it...
     
  4. Offline

    Byteflux

    "Null error" isn't very helpful. State the line relevant to your paste in which it's crapping itself.
     
  5. Offline

    darknesschaos

    it was the while line.
     
  6. You have to convert the line to lower case and trim it inside the while loop
    Code:
    while ((strLine = br.readLine()) != null){
       strLine = strLine .toLowerCase(Locale.ENGLISH).trim();
       plugin.gameOps.add(strLine);
    }
    
     
  7. Offline

    darknesschaos

    that would make total sense!.... now I feel so stupid.
     
Thread Status:
Not open for further replies.

Share This Page