The problem is that in your transpose method, both Matrix a and this refer to the same object. So you can try
1. Matrix a= new Matrix(...);//... since it depends on your constructor
or
You can loop the lower triangle of matrix and swap the element [i][j] with [j][i] since currently you are 'trying to' swap twice.
So make sure you don't swap twice and you are swapping correctly