 |
|
|
|
 |
stan229
Supreme tranceaddict
Registered: Jul 2006
Location: Brooklyn, NY
|
|
|
then i woudlnt be able to access firstOccurence cuz its private
unless theres another way?
also changin the thing to digitArray[z] == i doesn change anything, and im using that just to test if the value is repeated
the error i get is:
code:
Exception in thread "main" java.lang.NullPointerException
at homework6.PositionPair.Fuck(PositionPair.java:41)
at homework6.PositionPair.main(PositionPair.java:56)
|
|
Oct-10-2006 02:13
|
|
|
 |
 |
stan229
Supreme tranceaddict
Registered: Jul 2006
Location: Brooklyn, NY
|
|
|
yea so i modified the code.. had to create a ne class
i used your advice on the method so i have
code: int[] digitArray =
{ 0, 1, 1, 3, 4, 5, 5, 7, 8, 9 };
PositionPair[] positions = new PositionPair[10];
for (int i = 0; i < digitArray.length; i++)
{
for (int z = i + 1; z < digitArray.length; z++)
{
if (digitArray[z] == digitArray[i])
{
positions[i].setFirstOccurence(i);
positions[i].setSecondOccurence(z);
System.out.println(i);
System.out.println(z);
}
}
}
the prof juts sent us an email with the advice, but im ont sure if it even relates to me yet
| quote: |
To deal with the problem indicated below, we either have to initialize the array OR
check for null before accessing a particular location. I will expalin the former
solution in the assignment, right now.
Problem:
When checking to see if firstOccurrence equals -1 i
write "if(positions[num].getFirstOccurrence()==-1)" and when i run the
program it gives me a null pointer exception.
|
im confused lol
still getting a null pointer exception on the line that uses the setFirstOccurence method
|
|
Oct-10-2006 03:33
|
|
|
 |
 |
stan229
Supreme tranceaddict
Registered: Jul 2006
Location: Brooklyn, NY
|
|
|
tried a simple test and still got a nullPointerException..hmm
code:
positions[0].setFirstOccurence(5);
System.out.println(positions[0].getFirstOccurence());
the first line caused the exception.. wtf is going on lol
|
|
Oct-10-2006 04:22
|
|
|
 |
 |
ô§§|E
Supreme tranceaddict
Registered: Apr 2004
Location: Perth, Australia
|
|
|
Firstly, where's your default constructor for the PositionPair Class? cause at the moment you cant even create one of these objects right? So make one of those.
code:
public PostionPair()
{
firstOccurence = -1;
secondOccurence = -1;
}
Ur missing one lil concept here i think, the line ....
code:
PositionPair[] positions = new PositionPair[10];
Doesn't magically give you instantiations of 10 PositionPair objects, you still need to do this via ...
code:
for(int a = 0; a < 10; a++)
{
positions[a] = new PositionPair(); //You'll need a default constructor for this!
}
so now ur code looks like ....
code:
int[] digitArray = { 0, 1, 1, 3, 4, 5, 5, 7, 8, 9 };
PositionPair[] positions = new PositionPair[10];
//ADDED CODE
for(int a = 0; a < 10; a++)
{
positions[a] = new PositionPair(); //You'll need a default constructor for this!
}
for (int i = 0; i < digitArray.length; i++)
{
for (int z = i + 1; z < digitArray.length; z++)
{
if (digitArray[z] == digitArray[i])
{
positions[i].setFirstOccurence(i);
positions[i].setSecondOccurence(z);
System.out.println(i);
System.out.println(z);
}
}
}
Should do the trick by initialising the objects in the array first, let me know how that works for you
___________________
| quote: | Originally posted by eye_03
the reason why i dont do it is because i see people like you at clubs doped out of your mind. i say "shit, i dont wanna turn out like that scumbag" |
Last edited by ô§§|E on Oct-15-2006 at 12:17
|
|
Oct-15-2006 12:10
|
|
|
 |
 |
stan229
Supreme tranceaddict
Registered: Jul 2006
Location: Brooklyn, NY
|
|
|
thanks for everyones help i appreciate it.. lol i did
heres the final working code: 
code:
// Sorry that I put everything in the test class.
//I realized to change it in the last minute and don't have time to, code will still work though.
package homework6;
public class PositionTest
{
/**
* Find the minimum index for firstOccurence
*
* @param positions
* @return index
*/
public static int findMinimum(PositionPair[] positions)
{
if (positions.length == 0)
return -1;
int smallestVal = -1;
for (int i = 0; i < positions.length; i++)
{
if (positions[i].getSecondOccurence() != -1
&& (positions[i].getFirstOccurence() < smallestVal || smallestVal == -1))
smallestVal = positions[i].getFirstOccurence();
}
return smallestVal;
}
public static void main(String[] args)
{
int[] digitArray =
{ 0, 1, 2, 1, 2, 5, 5, 7, 8, 9 };
PositionPair[] positions = new PositionPair[10];
for (int i = 0; i < positions.length; i++)
positions[i] = new PositionPair();
// Loop to test for firstOccurence and secondOccurence
for (int i = 0; i < digitArray.length; i++)
{
if (positions[digitArray[i]].getFirstOccurence() == -1)
{
positions[digitArray[i]].setFirstOccurence(i);
} else if (positions[digitArray[i]].getSecondOccurence() == -1)
{
positions[digitArray[i]].setSecondOccurence(i);
}
}
// Prints out array
for (int i = 0; i < positions.length; i++)
{
System.out.println(positions[i].getFirstOccurence() + " "
+ positions[i].getSecondOccurence());
}
int n = findMinimum(positions);
System.out.println(n);
}
}
code:
package homework6;
public class PositionPair
{
private int firstOccurence = -1;
private int secondOccurence = -1;
public int getFirstOccurence()
{
return firstOccurence;
}
public void setFirstOccurence(int firstOccurence)
{
this.firstOccurence = firstOccurence;
}
public int getSecondOccurence()
{
return secondOccurence;
}
public void setSecondOccurence(int secondOccurence)
{
this.secondOccurence = secondOccurence;
}
}
Once again, thank you guys. I was up till very late in the morning working on this that day.
|
|
Oct-17-2006 12:21
|
|
|
 |
 |
|  |
All times are GMT. The time now is 07:32.
Forum Rules:
You may not post new threads
You may not post replies
You may not edit your posts
|
HTML code is ON
vB code is ON
[IMG] code is ON
|
|
|
|
|
|
Contact Us - return to tranceaddict
Powered by: Trance Music & vBulletin Forums
Copyright ©2000-2026, Jelsoft Enterprises Ltd.
Privacy Statement / DMCA
|