CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2012
    Posts
    3

    Need assistance with multi-method file writing to a txt file in a multi-class applet.

    Hey all, first time poster, so maybe you can help me with this -- I'm trying to get through the first portion of this assignment, but I am stumped with an error:

    "Exception in thread "main" java.lang.NullPointerException
    at sailBoat.goFast(homework4v3.java:87)
    at proj4.main(homework4v3.java:202)

    The code compiles just fine, and anything that isn't in the erroneous method seems to write to proj4.txt just fine, as I wanted to test that before I scrapped the code. That being said, I am wondering maybe if I should go with an interface (that'll be interesting to implement if it can be done) or perhaps rewriting the classes. I'm looking more for pointers in the right direction rather than having my code fixed for me, though any explanation will help. I am aware of some redundancies and logical inconsistencies (such as the f.exist portion, I just needed it to write.) in the code as well and will worry about cleaning those up / eliminating them as I get the file to write (and eventually read). Thanks in advance for your help!

    Code:
    import java.io.*;
    import java.util.*;
    
    abstract class Boat {
    
    	String boatName;
    	boolean sailUp;
    	
    	PrintStream p;
    	FileOutputStream fop;
    	File f;
    	
    	//f.createNewFile();		
    	//p.println("New File \"proj4.txt\" has been created to the current directory.");
    	
    	void setName(String name) { 
    	
    		boatName = name;
    	}
    	
    	void printName() {
    	
    		// p.println("This boat's name is " + boatName);
    		
    		p.println("This boat's name is " + boatName);
    		
    		
    		}
    		
    	abstract void goFast();
    	
    		/* p.println("Raising the sail");
    		sailUp = true; */
    		
    		
    		
    	abstract void goSlow();
    	
    	/*	p.printlin("Lowering the sail.");
    		sailUp = false; */
    	
    	
    /*	void raiseSail() { 
    		goFast();
    	}
    
    	void lowerSail() {
    		goSlow(); 
    		
    	} */
    	
    	abstract void whatIsBoatState();
    }
    		
    		
    class raceBoat extends Boat {		
    
    		String boatName;
    		boolean motoRev; // We will keep this as necessary.
    
    		void goFast() {
    			p.println("Throttle forward.");
    			motoRev = true;
    			}
    		
    		void goSlow() {
    			p.println("Throttle back.");
    			motoRev = false;
    			}
    			
    		void whatIsBoatState() {
    	
    		if (motoRev) { 
    			p.println("The throttle is forward!");
    		}
    		else {
    			p.println("The throttle is back!");
    		}
    	}
    }
    
    
    class sailBoat extends Boat {
    
    	void goFast() {
    	
    		p.println("Raising the sail");
    		sailUp = true;
    		
    		}
    		
    	void goSlow() {
    	
    		p.println("Lowering the sail.");
    		sailUp = false;
    	}
    	
    	void raiseSail() { 
    		goFast();
    	}
    
    	void lowerSail() {
    		goSlow(); 
    	
    	}
    	
    	void whatIsBoatState() {
    	
    		if (sailUp) { 
    			p.println("Sail up!");
    		}
    		else {
    			p.println("Sail down!");
    		}
    	}
    }
    
    class proj4 {
    
    	public static void main(String[] args) throws IOException { 
    
    		
    	FileOutputStream fop;
    	PrintStream p;
    	File f;
    			
    	Boat[] Boats;
    	sailBoat[] sailBoats;
    	raceBoat[] raceBoats;
    	char firstChar;
    	char secChar;
    	int i;
    	
    	f = new File("proj4.txt");
    	
    	Boats = new Boat[args.length];
    	sailBoats = new sailBoat[args.length];
    	raceBoats = new raceBoat[args.length];
    	
    	if(f.exists()) {
    	
    		FileWriter fstream = new FileWriter(f);
    		BufferedWriter out = new BufferedWriter(fstream);
    		
    		f.createNewFile();		
    		
    		fop = new FileOutputStream(f);
    		p = new PrintStream(fop);
    		
    		p.println("New File \"proj4.txt\" has been created to the current directory.");
    	
    		
    		// p.println("Gotta make sure something...");
    		
    		for (i = 0; i < args.length; i++) {
    	
    			firstChar = args[i].charAt(0);
    			secChar = args[i].charAt(1);
    		
    		if ((firstChar == 'B') || (firstChar == 'C') || (firstChar == 'N')) {
    	
    			raceBoats[i] = new raceBoat();
    			raceBoats[i].setName(args[i]);
    		
    			if ((secChar == 'a') || (secChar == 'e')) {
    		
    				raceBoats[i].goFast();
    				p.println("Boat number " + (i + 1) + "\n");
    		
    				p.print("  ");
    				raceBoats[i].printName();
    			
    				p.print("  ");
    				raceBoats[i].whatIsBoatState();
    			
    			}
    			
    		else {
    			
    			raceBoats[i].goSlow();
    			p.println("Boat number " + (i + 1) + "\n");
    		
    			p.print("  ");
    			raceBoats[i].printName();
    			
    			p.print("  ");
    			raceBoats[i].whatIsBoatState();
    			}
    		 
    		}
    	
    		else {
    	
    		sailBoats[i] = new sailBoat();
    		sailBoats[i].setName(args[i]);
    				
    		if ((secChar == 'a') || (secChar == 'e')) {
    		
    			sailBoats[i].goFast();
    			p.println("Boat number " + (i + 1) + "\n");
    		
    			p.print("  ");
    			sailBoats[i].printName();
    			
    			p.print("  ");
    			sailBoats[i].whatIsBoatState();
    			
    			}
    			
    			else {
    			
    			sailBoats[i].goSlow();
    			p.println("Boat number " + (i + 1) + "\n");
    		
    			p.print("  ");
    			sailBoats[i].printName();
    			
    			p.print("  ");
    			sailBoats[i].whatIsBoatState();
    			
    			
    			}
    					
    		}
    
    		
    		
    		}
    		
    		p.close();
    		p.println("File created successfully.");
    		}
    		else {
    		System.out.println("The Macho Man Randy Savage says this file already exists-ahhhhh! Appending the file-aaah!"); // Feature Not Implemented
    		}
    			
    	}
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Need assistance with multi-method file writing to a txt file in a multi-class app

    A null pointer exception generally means you are trying to use an object reference which hasn't been set to point at an object yet ie it's null.

    The error message tells you the problem is in the SailBoat classes goFast() method on line 87. I don't know which line is number 87 but given that there are only 2 lines and one of them deals with a primitive boolean type it must be the other line. The only thing that can be null on the other line is 'p' so you must be calling the goFast() method before you have assigned p to an object of type PrintStream.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Mar 2012
    Posts
    3

    Re: Need assistance with multi-method file writing to a txt file in a multi-class app

    Thanks, I'll try see about declaring p elsewhere. What I'm trying to do is writing to a text file with a command line fed array. Not the most elegant solution, but it's only part of a step to a much larger project.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured