我得到了一个分配,写一个类数组和类Matrix,矩阵类将使用数组类来创建它的矩阵,它就像一个指向其他数组的数组,我的问题是一切都很好但是当我试图添加两个我创建的矩阵时,我一直有NullPointerException,这是我的整个代码。我在下面的代码中指出了我的错误,谢谢。
import java.util.Scanner;
public class Matrix {
public Vector mat[];
public static int m;
static int i, j;
Scanner sc;
Matrix() {
m = 0;
mat = null;
}
Matrice(int m, int n) {
Matrix.m = m;
Vector.n = n;
mat = new Vector[100];
// mat[i]=new Vector(100);
}
public void fillmatrix() {
mat = new Vector[100];
sc = new Scanner(System.in);
System.out.println("enter the number of the rows");
Matrix.m = sc.nextInt();
System.out.println("enter the number of your columns");
Vector.n = sc.nextInt();
for (j = 0; j < m; j++) {
mat[j] = new Vector(Vector.n);
mat[j].fillmatrix();
}
}
public void DisplayMatrix() {
System.out.println("Th matrix you have entered: ");
for (i = 0; i < Vector.n; i++) {
mat[i].Display();
}
public static Matrix Addition(Matrix mat1, Matrix mat2) {
Matrix mat3 = new Matrix();
for (j = 0; j < Vector.n; j++) {
for (i = 0; i < Matrix.m; i++) {
mat3.mat[i].t[j] = (mat1.mat[i].t[j]) + (mat2.mat[i].t[j]); < -- -- -- -- - where the mistake but i can 't solve it ://>
}
}
return mat3;
}
}
public static void main(String args[]) {
Matrix Mat1 = new Matrix();
Matrix Mat2 = new Matrix();
Matrix Mat3 = new Matrix();
Mat1.fill();
Mat1.affiche2();
Mat2.fill();
Mat2.affiche2();
Mat3 = Matrix.Addition(Mat1, Mat2);
Mat3.affiche2();
}
import java.util.Scanner;
public class Vector {
public static int n;
static int i;
static int j, k;
int t[];
static Scanner sc = new Scanner(System.in);
Vector(int n) {
Vector.n = n;
t = new int[100];
}
Vector() {
Vector.n = 0;
t = null;
}
public void fill() {
t = new int[100];
sc = new Scanner(System.in);
for (i = 0; i < n; i++) {
System.out.println("enter " + i + "° element of your array");
t[i] = sc.nextInt();
}
}
public void contain(Vector Tab) {
i = 0;
j = 0;
if (n <= Tab.t.length) {
while (j < Tab.t.length) {
if (t[i] != Tab.t[j]) {
j++;
i = 0;
} else {
i++;
j++;
}
}
if (i > n) {
System.out.println("the array 1 is contained in array 2");
}
els
System.out.println("the array 1is not contained in array2");
}
} else {
while (i < n) {
if (t[i] != Tab.t[j]) {
i++;
j = 0;
} else {
i++;
j++;
}
}
if (j > n) {
System.out.println("the array 2 is contained in array 1");
} else {
System.out.println("the array 2 is contained in array 1");
}
}
}
boolean appartient(int x) {
i = 0;
boolean found = false;
while ((i < n) && (t[i] != x)) {
i++;
}
if (i >= n)
System.out.println("le x n'appartient pas a votre Tableau");
else {
System.out.println("le x appartient a votre tableau");
found = true;
}
return found;
}
public void Display() {
for (i = 0; i < n; i++) {
System.out.println(t[i]);
}
}
public void Order() {
int j, tmp;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (t[i] > t[j]) {
tmp = t[i];
t[i] = t[j];
t[j] = tmp;
}
}
}
for (int i = 0; i < n; i++) {
System.out.println("your array ordered " + t[i]);
}
}
void inser(int x) {
if (appartient(x) == true) {
System.out.println("Insersion is impossible, the value already exist");
} else {
for (i = n;
(i > 0) && (t[i - 1] > x); i--)
t[i] = t[i - 1];
t[i] = x;
n++;
for (i = 0; i < n; i++)
System.out.println(t[i]);
}
}
public static void fusionner(Vector tab1, Vector tab2) {
Vector FUS = new Vector(tab1.t.length + tab2.t.length);
i = 0;
j = 0;
k = 0;
while ((i < tab1.t.length) && (j < tab2.t.length))
if (tab1.t[i] < tab2.t[j]) {
FUS.t[k] = tab1.t[i];
k++;
i++;
} else {
FUS.t[k] = tab2.t[j];
k++;
j++;
}
while (i < n) {
FUS.t[k] = tab1.t[i];
k++;
i++;
}
while (j < n) {
FUS.t[k] = tab2.t[j];
k++;
j++;
}
System.out.println("your arrays after the fusion is ");
for (k = 0; k < tab1.t.length + tab2.t.length; k++)
System.out.println(FUS.t[k]);
}
public static void main(String args[]) {
Vector tab1 = new Vector();
System.out.println("Enter n");
n = sc.nextInt();
tab1.fill();
tab1.Display();
tab1.trier();
Vector tab2 = new Vector();
tab2.fill();
tab2.affiche();
tab2.trier();
fusionner(tab1, tab2);
tab1.contain(tab2);
tab2.appartient(5);
tab1.inser(6);
}
}
答案 0 :(得分:0)
我决定把它作为答案而不是评论,因为它看起来很杂乱。
当您编写Matrix mat3 = new Matrix();
时,您正在调用构建函数Matrix()
,您将找到它,设置mat = null;
。所以,只要你写一些mat3.mat[i].**something**
,你就会找[null].**something**
,这没有多大意义。
我推测在初始化fillmatrix()
后调用mat3
可能是您可能正在寻找的内容。
希望我能告诉你哪里出错了。
答案 1 :(得分:0)
Vecteur class ..
import java.util.Scanner;
public class Matrice {
public Vecteur mat[];
public int m, n;
static int i, j;
Matrice() {
m = 0;
mat = null;
}
Matrice(int m, int n) {
this.m = m;
this.n = n;
mat = new Vecteur[n];
for (int i = 0; i < n; i++) {
mat[i] = new Vecteur(m);
}
}
Scanner sc = new Scanner(System.in);
public void remplir() {
mat = new Vecteur[m];
for (j = 0; j < m; j++) {
mat[j] = new Vecteur(this.n);
mat[j].remplir();
}
public void affiche2() {
System.out.println("L'affichage de votre matrice");
for (j = 0; j < this.m; j++) {
System.out.println("La ligne" + j);
mat[j].affiche();
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
Matrice Mat1 = new Matrice(100, 100);
System.out.println("donner le nombre de votre Ligne a lire de la matrice1");
Mat1.m = sc.nextInt();
System.out.println("donner le nombre de votre Colomne a lire de la matrice1");
Mat1.n = sc.nextInt();
Mat1.remplir();
Mat1.affiche2();
Matrice Mat2 = new Matrice(100, 100);
System.out.println("donner le nombre de votre Ligne a lire de la matrice2");
Mat2.m = sc.nextInt();
System.out.println("donner le nombre de votre Colomme a lire de la matrice2");
Mat2.n = sc.nextInt();
Matrice Mat3 = new Matrice(100, 100);
Mat2.remplir();
Mat2.affiche2();
Mat3 = Matrice.somme(Mat1, Mat2);
Mat3.affiche2();
}
public static Matrice somme(Matrice mat1, Matrice mat2) {
Matrice mat3 = new Matrice(mat1.m, mat1.n);
for (int j = 0; j < mat1.n; j++) {
for (int i = 0; i < mat1.m; i++) {
mat3.mat[i].t[j] = (mat1.mat[i].t[j]) + (mat2.mat[i].t[j]);
}
}
return mat3;
}
}
}
Matrix class ..
<style>
body
{
width:100%; height:100%;margin:0;padding:0;font:12pt Tahoma
}
*{
box-sizing:border-box;-moz-box-sizing:border-box
}
.page
{
width:210mm;min-height:297mm;padding:10mm;margin:10mm auto;
border:1px #D3D3D3 solid;border-radius:5px;
box-shadow:0 0 5px rgba(0,0,0,0.1)}
.subpage{height:257mm;
}
@page{ size:A4;margin:0 }
@media {
print{
html,body{width:210mm;height:297mm}.page{margin:0;
border:initial;border-radius:initial;width:initial;
min-height:initial;
box-shadow:initial;background:initial;page-break-after:always
} }
</style>