SwiftStriker00's Profile
Reputation: 429
Architect
- Group:
- Expert w/DIC++
- Active Posts:
- 1,595 (0.99 per day)
- Joined:
- 25-December 08
- Profile Views:
- 21,609
- Last Active:
Today, 06:49 AM- Currently:
- Offline
Previous Fields
- Country:
- US
- OS Preference:
- Windows
- Favorite Browser:
- Chrome
- Favorite Processor:
- Intel
- Favorite Gaming Platform:
- PC
- Your Car:
- Mazda
- Dream Kudos:
- 275
Latest Visitors
-
macosxnerd101 
01 May 2013 - 22:29 -
Gungnir 
30 Apr 2013 - 07:16 -
trupicc 
30 Apr 2013 - 07:08 -
darek9576 
30 Apr 2013 - 06:45 -
farrell2k 
30 Apr 2013 - 06:16
Posts I've Made
-
In Topic: How do I get rid of a null pointer exception?
Posted 16 May 2013
--
Bit of further explanation of locality. The reason we can use the same variable name in different methods is because of the scope of a variable. in Java it is pretty simple to tell in the basic case because a variable will stay in scope until the code block is complete (e.g.{ }). Take the following code. It will break because I try and access the variable out of scope
if( true ) { int a = 5; } System.out.println( a );
Now when dealing with class variables and constructors we usually want to keep our terminology the same to make things easier. So if we create a new Person( "Sam" ). we would probably write a class like this:
public class Person{ private String name; public Person( String name ) { //what do we put here? } }
So how do we know which 'name' to use? Well if you don't explicitly tell Java, it will always use the most local one. Another way of saying is the more recently created one inside that block (in the class above, it would be the parameter to the constructor). So in order to maintain the similar terminology and state which to save we need the keyword this.
public class Person{ private String name; public Person( String name ) { this.name = name; } }
this, is talking about "this class" essentially, and will distinguish that we want to save the parameter 'name' to the class 'name'. -
In Topic: How do I get rid of a null pointer exception?
Posted 16 May 2013
You are getting a null pointer exception because you your constructor has the line int[][] board = .... Because you put the int[][] java thought you were creating a new local variable for that method. So you are not actually saving the new to the class variable board.
Just delete the int[][] in your constructor -
In Topic: Can anyone explain what is wrong with this code ?
Posted 13 May 2013
Syntactically speaking your code is fine. But as GregBrannon said, you need to tell us what you are expecting versus what you are actually getting. -
In Topic: Phone Directory program
Posted 8 May 2013
You still should separate out the methods, you don't need to create static variables and such. lets combine what pbl and I said, using the SelectionSort class. I heavily commented the code, so read most of it there. I left stuff out for you to finish however...
public class Final { public static void main( String[] args ) { //We set the inputfile here, we want to remove as much "hard code" as possible //it would be better if it was passed in from args, but that is another lesson string inputFile = "numbers.txt"; //We are going to create an instance of your Final class, it will handle everything //in your program. If you are not going to do anything with the instance. You can //simply write "new Final(intputFile);" and thats it. Final clientObject = new Final( inputFile ); } //These are the member/class variables of your client. We will need to instantiate them //in your constructor. The constructor is run when you call new Final(...) private String inputFile; private BuisnessDirectory[] buisnessNumbers; //This is our constructor, you can see it has a parameter for a string with the name of //the file we want to read. You can see the steps the constructor will do. First it will //set the member variable to the one that was passed in, that way we can save it for later //use. Next step we will verify it exists. Because we saved the file to inputFile member //variable, we can use inputFile in the verifyFileExists method without needing more //parameters. The same goes with the readInFile method as well. However you can see that //instead of returning a temp array, we can just create the buisNumbers array right there //because it too is a member variable now. Lastly you can create an object of pbl's select //sort class, pass in the buisnessNubmer array, sort it, then save the sorted arrayt back //to the member variable. public Final( String file ) { inputFile = file; //Read in the data verifyFileExists(); readInFile(); //Sort the data SelectSort sorter = SelectSort( buisnessNumbers, buisnessNumbers.length ); buisnessNumbers = sorter.doTheSort(); } public BuisnessDirectory[] getDirectory() { return buisnessNumbers; } public void verifyFileExists() { File input = new File( inputFile ); if( !input.exists() ) { System.out.println( "File " + file + " does not exist!" ); System.exit(); } } public void readInFile() { buisnessNumbers = new BuisnessDirectory[25]; //bad hardcode, fix //read in your data, save to member variable } }
Edit: formatting -
In Topic: Can anyone help me with this job assignment program?
Posted 8 May 2013
Well. You might need to chalk this one up to a lesson in procrastination. You can't just whip up code the night before and expect not to run into issues.
You need to create a new Worker object, then save that object to the index of the array you are working with.
If I was going to create one worker I would do this: Worker hardWorker = new Worker( "bob", "construction", 7.55 ); think about that and what andrewsw and I said.
Also read up this tutorial on help on fixing your syntax errors: Beginners, Fix Your Own Java Errors
My Information
- Member Title:
- Microsoft Insider
- Age:
- 25 years old
- Birthday:
- May 7, 1988
- Gender:
-
- Location:
- Rochester, NY
- Interests:
- Programming, Snowboarding, Videogames, Football, Camping, Watchin Movies
- Full Name:
- Ryan Bucinell
- Years Programming:
- 6
- Programming Languages:
- C#, Java, C++, Actionscript 3.0, Scheme, Perl, Batch, Powershell, Javascript
Contact Information
- E-mail:
- Click here to e-mail me
- AIM:
-
SwiftStriker00
- Website URL:
-
http://cs.rit.edu/~rmb1201/
- Skype:
-
SwiftStriker00
- LinkedIn:
- http://www.linkedin.com/in/ryanbucinell
- Facebook:
- http://www.facebook.com/ryanbucinell
- Twitter:
- https://twitter.com/SwiftStriker00
|
|


Find Topics
Find Posts
View Reputation Given




|
Comments
SwiftStriker00
03 Nov 2012 - 12:08pagelarry
03 Nov 2012 - 06:38