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

    Default Stupid mistake, maybe?

    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package samething1;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.List;
    
    /**
     *
     * @author JSM
     */
    public class SameThing1 {
        static File in;
        static String temp;
        static File out;
        static String[] Write;
        static String[] list;
        static int lastint = 0;
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws InterruptedException, FileNotFoundException, IOException {
            System.out.println("Started");
            
            Thread.sleep(5000);
            in = new File(getCurrentPath() + File.separator + "in1.txt");
            out = new File(getCurrentPath() + File.separator + "out1.txt");
            if (!in.exists()) {
                System.out.println("in1.txt doesn't exist");
                System.out.println(in.toString());
                Thread.sleep(5000);
                System.exit(0);
            }
            else {
            
            String tstring;
            BufferedReader ini = new BufferedReader(new FileReader(in));
            boolean done = false;
            try {
            while (!done) {
            
            tstring = ini.readLine();
            System.out.println("Read: " + tstring);
            if (!contains(tstring)) {
            add(tstring);
            }
            else {System.out.println(tstring + " has already been found, skipping");
            }
            
            }
            
            } catch (Exception Ex) {
            Ex.printStackTrace();
            done = true; 
            System.out.println("Reached end of list. Writing output");
            BufferedWriter outs = new BufferedWriter(new FileWriter(out));
            int tint = 0;
            
            
    
            
            int tint2 = 0;
            if (out.exists()) {out.delete();}
            try {
            while (tint != tint2) {
            outs.write(Write[tint2]);
            outs.newLine();
            tint2++;
            }
            } catch (Exception ex2) {}
            }
            
            
            }
            
            
            
            
        }
        
        
        public static String getCurrentPath()
    {
        try {
        temp = new File(".").getCanonicalPath();
        System.out.println(temp);
        } catch (IOException e) {
            
            return null;
        }
        return temp;
    }
        
        
        public static void add(String str) {
            lastint++;
            list[lastint] = str;
            
            }
        public static boolean contains(String str) {
        for (String tstr : list) {
            if (str.equals(tstr)) {
            return true;
            }
            
        }
        return false;
            
            }
    }
    Hi, I'm trying to make a simple program that will read through a list and check and remove any repeated lines. However, it only reads and checksthe first line before getting a NullPointerException error while checking it under the "contains" void. Help is appreciated, thanks.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Stupid mistake, maybe?

    Full stack trace please! And maybe say which line of your code it is referring to because we're all lazy here and don't like counting to 110 or whatever.

Similar Threads

  1. Replies: 0
    Last Post: 02-16-2011, 12:16 AM
  2. I need an idea with something really stupid
    By blf_titi in forum Threads and Synchronization
    Replies: 2
    Last Post: 10-27-2010, 04:24 AM
  3. Stupid error
    By dewitrydan in forum Java Applets
    Replies: 3
    Last Post: 08-09-2010, 02:29 PM
  4. incredibally stupid....
    By tek0011 in forum New To Java
    Replies: 7
    Last Post: 11-26-2009, 10:35 AM
  5. a really stupid question
    By SwinGirl in forum NetBeans
    Replies: 10
    Last Post: 06-25-2008, 10:06 PM

Posting Permissions

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