hi all!!

I need some help here. It's the Translation matrix. I can't seem to get the output. I wonder if my conditions are right cause I've been changing them. Please do take a look from line 125 onwards. Thanks much!

import java.io.*;
import java.util.*;
import java.text.*;




public class KateAug24{
	
   	private static StringBuffer buffer;
   	private static BufferedReader input1=null;
   	
   
   	public static String [] arrayM1 = new String [10000];
   	public static String [] arrayM2 = new String [10000];
   	public static Double [][] coordinates1 = new Double [10000][10];
   	public static Double [][] coordinates2 = new Double [10000][10];
   	public static Double [][] coordinates2T = new Double [10000][10];
   	public static Double [] Translate = new Double [10]; 
    
   	public static Double [][] coordinatesDiff = new Double [10000][3]; 
  
   	static DecimalFormat fmt= new DecimalFormat("0.000"); //3 D.P
	
	public static void main(String args[]) {
	 	
	 	
	int i=0,j=0;	
   	String text1;
   	String text2;
   	double X=0;
   	double Y=0;
   	double Z=0;

    try{


    FileInputStream fstream1 = new FileInputStream("D:\\project1\\exp\\proteins\\1A6G.pdb.txt");
    FileInputStream fstream2 = new FileInputStream("D:\\project1\\exp\\proteins\\1A6K.pdb.txt");
    DataInputStream in1 = new DataInputStream(fstream1); // Get the object of DataInputStream
    DataInputStream in2 = new DataInputStream(fstream2); // Get the object of DataInputStream
    BufferedReader br1 = new BufferedReader(new InputStreamReader(in1));
    BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));

    while ((text1 = br1.readLine()) != null)   {  //Read File Line By Line

        if(text1.trim().startsWith("ATOM")) {
        	StringTokenizer s1 = new StringTokenizer(text1," ");

         	int counter1=0;
          	String line1="";

          	while(s1.hasMoreTokens()) {

				String ss1 = s1.nextToken();
		    	counter1++;

		    	if(counter1 == 3){
		    		line1 += ss1;
		    		arrayM1[i] = ss1;
		    	}
		    }

    		coordinates1[i][0]= Double.parseDouble(text1.substring(30,38));//X
			coordinates1[i][1]= Double.parseDouble(text1.substring(38,46));//Y
			coordinates1[i][2]= Double.parseDouble(text1.substring(46,56));//Z



			i+=1;
		}//end of if


	} //end of while.

		
			
			
			
	i=0;

	while ((text2 = br2.readLine()) != null)   {  //Read File Line By Line

        if(text2.trim().startsWith("ATOM")) {
        	StringTokenizer s2 = new StringTokenizer(text2," ");

         	int counter2=0;
          	String line2="";

        	while(s2.hasMoreTokens()) {

				String ss2 = s2.nextToken();
		    	counter2++;

		    	if(counter2 == 3){
		    		line2 += ss2;
		    		arrayM2[i] = ss2;
		    	}
			}

    		coordinates2[i][0]= Double.parseDouble(text2.substring(30,38));//X
			coordinates2[i][1]= Double.parseDouble(text2.substring(38,46));//Y
			coordinates2[i][2]= Double.parseDouble(text2.substring(46,56));//Z
		
			 i+=1;	
				
		
		 }//end of if


	} //end of while.

			}//end of try



			catch( IOException e) {
				e.printStackTrace();
				}//end of catch

//------------------------------------------------------------------------------				
				
				
				
				for(int a=0; a<coordinates1.length; a++) {
         for(int b=0; b<coordinates1[a].length; b++) { 
          for(int c=0; c<coordinates1[b].length ; c++){
         
      
         
            X = coordinates1[1][0]/coordinates2[1][0];
			Y = coordinates1[1][1]/coordinates2[1][1];
			Z = coordinates1[1][2]/coordinates2[1][2];
         
         double [][] Translation1={ {X,0,0}, {0,Y,0}, {0,0,Z} };
         
         coordinates2[a][b]= coordinates2[a][c]*Translation1[c][b];
         
        
         
      if(coordinates1[a][b]!= null)
	System.out.println(coordinates1[a][b]); //Print out the whole array
	}
 }
}





	}// end of main
	}

I can't seem to get the output

Can you explain what happens?

Try printing out the values of
coordinates1[a].length
and
coordinates1.length
to see what is happening in the code.

Can you explain what happens?

Try printing out the values of
coordinates1[a].length
and
coordinates1.length
to see what is happening in the code.

for the 1st one I got:

10
Exception in thread "main" java.lang.NullPointerException
10
10
at KateAug244.main(KateAug244.java:137)
Java Result: 1

2nd one: the same as the above.

at KateAug244.main(KateAug244.java:137)
What code is at line 137? What variable is null?
There appears to be more than one.
Add a println() to show the contents of all the variables to see which one is null.

at KateAug244.main(KateAug244.java:137)
What code is at line 137? What variable is null?
There appears to be more than one.
Add a println() to show the contents of all the variables to see which one is null.

137: coordinates2[a]= coordinates2[a][c]*Translation1[c];

o/p: coordinates2[a]

-3.7830557977332173
0.0
0.0
Exception in thread "main" java.lang.NullPointerException
at KateAug24.main(KateAug24.java:137)
Press any key to continue...


o/p: coordinates2[a][c]

-3.7830557977332173
15.273
13.451
Exception in thread "main" java.lang.NullPointerException
at KateAug24.main(KateAug24.java:137)
Press any key to continue...


o/p: Translation1[c]

1.05231037489102
0.0
0.0
Exception in thread "main" java.lang.NullPointerException
at KateAug24.main(KateAug24.java:137)
Press any key to continue...

What variable was null? The JVM is finding something null at line 137

Your print out of numbers without labels doesn't mean anything to me.
A better printout would be to add id strings to the output:
System.out.prntln("Translation1[c]= " + Translation1[c]);

Ok,

I cannot print coordinates2[a][c], Translation1[c], coordinates2T[a]. It gives the same error. Nothing comes out eversince I flush the memory. That is the only thing in my command prompt I see

It gives the same error

please post the full text of the error message.

please post the full text of the error message.

Sure! Error msg:

Exception in thread "main" java.lang.NullPointerException
at KateAug24.main(KateAug24.java:137)
Press any key to continue...

Test all the variables used on line 137 for null.

Try changing the types from Double to double for all the arrays but the variable: coordinates1 which you need to be an Object for testing != null
Then you shouldn't get NPEs for primitives.

Thank you! I've changed it but there's a new error..

coordinates2T[a]=-3.7830557977332173
coordinates2T[a]=-3.7830557977332173
coordinates2T[a]=-3.7830557977332173
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at KateAug24.main(KateAug24.java:137)
Press any key to continue...

Thank you. That error is gone but, unfortunately another one pops up:

coordinates2T[a]=-3.7830557977332173
coordinates2T[a]=-3.7830557977332173
coordinates2T[a]=-3.7830557977332173
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at KateAug24.main(KateAug24.java:137)
Press any key to continue...

What is the size of the array: Translation1?
Does it have an element at index 3?

What is the size of the array: Translation1?
Does it have an element at index 3?

I dont think I have declared any size for Translation1..
I just create Translation1:

     double [][] Translation1={ {X,0.0,0.0}, {0.0,Y,0.0}, {0.0,0.0,Z} };

Must I declare a specific size?

Member Avatar for coil

Well, at this point, the array is of length 3 in both dimensions. So if you are calling Translation1[3][0] or Translation1[0][3], (or with any number as index > 2), you well get an error.

You should copy and paste the definition from your code, not type it in. Your original code has:
double [][] Translation1={ {X,0,0}, {0,Y,0}, {0,0,Z} };
It creates a 3x3 array.

You should copy and paste the definition from your code, not type it in. Your original code has:
double [][] Translation1={ {X,0,0}, {0,Y,0}, {0,0,Z} };
It creates a 3x3 array.

I changed that part of my codes, because I initialized it with a double so I realized the 0 should be a 0.0
And yes, I want to create a 3x3 array, because I want an orthogonal matrix.

Do you understand now why you get: ArrayIndexOutOfBoundsException: 3

Do you understand now why you get: ArrayIndexOutOfBoundsException: 3

not really, my guess would be my 3x3 matrix? But why? Please enlighten me, I'm still trying to understand the world of Java...

Ok, so now I finaly got rid of the error ,my code became like this:

for(int a=0; a<coordinates1.length; a++) {
       for(int b=0; b<2; b++){ 
         for(int c=0; c<1; c++){
         
      
         
            X = coordinates1[1][0]/coordinates2[1][0];
			Y = coordinates1[1][1]/coordinates2[1][1];
			Z = coordinates1[1][2]/coordinates2[1][2];
         
         double [][] Translation1={ {X,0.0,0.0}, {0.0,Y,0.0}, {0.0,0.0,Z} };
         
         coordinates2T[a][b]+= coordinates2[a][c]*Translation1[c][b];
         
   if( coordinates2T[a][b]!= 0.0)
System.out.println("\tcoordinates2T[a][b]="+coordinates2T[a][b]);
 //Print out the whole array
         	
    	
}
}
}

But when I print it, this is the output(Part of it, codes are too long):


coordinates2T[a]=0.0
coordinates2T[a]=6.621136878814299
coordinates2T[a]=0.0
coordinates2T[a]=5.331004359197908
coordinates2T[a]=0.0
coordinates2T[a]=7.217796861377507
coordinates2T[a]=0.0
coordinates2T[a]=4.471266782911944
coordinates2T[a]=0.0
coordinates2T[a]=8.491092414995641
coordinates2T[a]=0.0
coordinates2T[a]=4.902714036617263
coordinates2T[a]=0.0
coordinates2T[a]=1.1080828247602441
coordinates2T[a]=0.0

Press any key to continue...


And when I print it individually:

coordinates2T[1][0]=-3.621
coordinates2T[1][1]=0.0
coordinates2T[1][2]=0.0

So, this means that i have a zero value for both the 2nd column and 3rd column.
Why is that so? your help is greatly appreciated! Thank you!

I don't know why for sure. Try adding some debug info to the print out to show a and b:
System.out.println("\tcoordinates2T[a]="+coordinates2T[a] + " a=" + a + ", b=" + b);

I don't know why for sure. Try adding some debug info to the print out to show a and b:
System.out.println("\tcoordinates2T[a]="+coordinates2T[a] + " a=" + a + ", b=" + b);

Thank you!

Alrighty, I've got all the values I need, however,

There are repeititons in my o/p:
The first column(X) will repeat 3 times
The second column(Y) will repeat twice
The third column(Z) will not repeat at all, it stays at 1

All of the above happens when: coordinates2T[a]+= coordinates2[a][c]*Translation1[c];

(Part of my o/p)


coordinates2T[a]=4.903
coordinates2T[a]=4.903
coordinates2T[a]=4.903
coordinates2T[a]=35.420
coordinates2T[a]=35.420
coordinates2T[a]=-4.393
coordinates2T[a]=1.108
coordinates2T[a]=1.108
coordinates2T[a]=1.108
coordinates2T[a]=27.713
coordinates2T[a]=27.713
coordinates2T[a]=-3.610
Press any key to continue...


However if I remove the + sign infront of the = it doesn't repeat, which is great but then again,
I am unable to print coordinates2T[1][0], coordinates2T[1][1] but I can print coordinates2T[1][2].

Is it possible If I want my o/p to be able to print w/o repetition and also I can print individual coordinates as and when I want to?


for(int a=0; a<coordinates1.length; a++) {
       for(int b=0; b<3; b++){ 
         for(int c=0; c<3; c++){
         
      
         
            X = coordinates1[1][0]/coordinates2[1][0];
			Y = coordinates1[1][1]/coordinates2[1][1];
			Z = coordinates1[1][2]/coordinates2[1][2];
         
         double [][] Translation1={ {X,0.0,0.0}, {0.0,Y,0.0}, {0.0,0.0,Z} };
         
         coordinates2T[a][b]+= coordinates2[a][c]*Translation1[c][b];
         
if( coordinates2T[a][b]!= 0.0)

 System.out.println(fmt.format(coordinates2T[a][b]));
 //Print out the whole array

I don't see where you print this string out in the code you just posted:
"coordinates2T[a]=4.903"

Try adding some debug info to the print out to show a and b:
System.out.println("\tcoordinates2T[a]="+coordinates2T[a] + " a=" + a + ", b=" + b);

System.out.println(fmt.format(coordinates2T[a]))
The above print out doesn't have an id string with the number

I don't see where you print this string out in the code you just posted:
"coordinates2T[a]=4.903"

Try adding some debug info to the print out to show a and b:
System.out.println("\tcoordinates2T[a]="+coordinates2T[a] + " a=" + a + ", b=" + b);

System.out.println(fmt.format(coordinates2T[a]))
The above print out doesn't have an id string with the number

Alright, I think I've got it!! Thank you for your guidance and help!! :D
May God bless you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.