SwiftStriker00's Profile User Rating: *****

Reputation: 433 Architect
Group:
Expert w/DIC++
Active Posts:
1,596 (0.77 per day)
Joined:
25-December 08
Profile Views:
28,637
Last Active:
User is offline Aug 25 2014 12:47 PM
Currently:
Offline

Previous Fields

Country:
US
OS Preference:
Windows
Favorite Browser:
Chrome
Favorite Processor:
Intel
Favorite Gaming Platform:
PC
Your Car:
Jeep
Dream Kudos:
275

Latest Visitors

Icon   SwiftStriker00 has set their status

Posts I've Made

  1. 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'.
  2. 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
  3. 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.
  4. 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
  5. 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:
No idea why my code works
Age:
26 years old
Birthday:
May 7, 1988
Gender:
Location:
Rochester, NY
Interests:
Programming, Snowboarding, Videogames, Football, Camping, Watching 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
Website URL:
Website URL  http://cs.rit.edu/~rmb1201/
Skype:
Skype  SwiftStriker00
LinkedIn:
http://www.linkedin.com/in/ryanbucinell
Facebook:
http://www.facebook.com/ryanbucinell
Twitter:
https://twitter.com/SwiftStriker00

Comments

Page 1 of 1
  1. Photo

    SwiftStriker00 Icon

    07 Sep 2013 - 09:27
    Monu, welcome to d.I.c, if you need help please post your problem in the Java forum along with the code you have already written. We can help you there
  2. Photo

    monu63 Icon

    07 Sep 2013 - 06:50
    hi sir i am a begineer in java.while doing my Thread problem i geting and error of illegal staticdeclaration error. i need know when it usally occur..
  3. Photo

    SwiftStriker00 Icon

    03 Nov 2012 - 12:08
    hi pagelarry, welcome to </d.i.c>
  4. Photo

    pagelarry Icon

    03 Nov 2012 - 06:38
    Hi SwiftStriker..
Page 1 of 1