我在addPointLoad()方法中遇到了空指针异常。

huangapple go评论92阅读模式
英文:

I'm getting a null pointer exception in addPointLoad() method

问题

我有两个对象类,我正在尝试通过这种方法访问它们,但是它会抛出空指针异常。不知道是数组大小的问题还是更复杂的问题。有人建议以以下方式增加数组大小(大小 x 1.5),但我不知道如何做到这一点。

下面是 Beam 类:

public abstract class Beam {
	private String name;
	protected double length; // [m]
	protected double Emod; // [MPa]
	protected double Ixx; // [m^4]
	private LoadModel loadModel;

	public Beam(String name, double length, double Emod, double Ixx) {
		this.name = name;
		this.length = length;
		this.Emod = Emod;
		this.Ixx = Ixx;
	}
	public void addPointLoad(double magnitude, double loadPos) {
		PointLoad PL = new PointLoad(magnitude, loadPos);
		loadModel.addPL(PL);
	}
}

LoadModel 类如下:

public class LoadModel {
	private PointLoad[] pointLoads;
	private LineLoad[] lineLoads;
	public int plCounter;
	public int llCounter;

	public LoadModel(PointLoad[] pl, LineLoad[] ll) {
		pointLoads = new PointLoad[2];
		lineLoads = new LineLoad[2];
		plCounter = 0;
		llCounter = 0;
	}

	public void addPL(PointLoad pl) {
		if (plCounter < pointLoads.length) {
			pointLoads[plCounter] = pl;
			plCounter++;
		} else {
			plCounter++;
			pointLoads[plCounter] = pl;
			plCounter++;
		}
		System.out.println("点载荷的数量已增加");
	}

	public int numPL() {  //我添加了这个,因为我想追踪点载荷的数量
		return plCounter;
	}
}

PointLoad 类如下:

public class PointLoad {
	private double magnitude;
	private double loadPos;
	
	public PointLoad(double magnitude, double loadPos) {
		this.magnitude = magnitude;
		this.loadPos = loadPos;
	}
	
	public double getMag() {
		return magnitude;
	}
	
	public double getLoadPos() {
		return loadPos;
	}
}

任何帮助将不胜感激。

英文:

I have 2 object classes that I am trying to access via this method, but it gives a null-pointer exception. Don't know if it is simply the array size or something more complicated. I was advised to increase the array size the following way (size x 1.5) but am clueless as to how can I do this.

Below is class Beam:

public abstract class Beam {
	private String name;
	protected double length; // [m]
	protected double Emod; // [MPa]
	protected double Ixx; // [m^4]
	private LoadModel loadModel;

	public Beam(String name, double length, double Emod, double Ixx) {
		this.name = name;
		this.length = length;
		this.Emod = Emod;
		this.Ixx = Ixx;
	}
	public void addPointLoad(double magnitude, double loadPos) {
		PointLoad PL = new PointLoad(magnitude, loadPos);
		loadModel.addPL(PL);
	}

The LoadModel class is below:

public class LoadModel {
	private PointLoad[] pointLoads;
	private LineLoad[] lineLoads;
	public int plCounter;
	public int llCounter;

	public LoadModel(PointLoad[] pl, LineLoad[] ll) {
		pointLoads = new PointLoad[2];
		lineLoads = new LineLoad[2];
		plCounter = 0;
		llCounter = 0;
	}

	public void addPL(PointLoad pl) {
		if (plCounter &lt; pointLoads.length) {
			pointLoads[plCounter] = pl;
			plCounter++;
		} else {
			plCounter++;
			pointLoads[plCounter] = pl;
			plCounter++;
		}
		System.out.println(&quot;The amount of Point Loads have been increased&quot;);
	}

	public int numPL() {  //Added this aswell as I want to keep track of the number of point loads
		return plCounter;
	}

Below is the PointLoad class:

public class PointLoad {
	private double magnitude;
	private double loadPos;
	
	public PointLoad(double magnitude, double loadPos) {
		this.magnitude = magnitude;
		this.loadPos = loadPos;
	}
	
	public double getMag() {
		return magnitude;
	}
	
	public double getLoadPos() {
		return loadPos;
	}
}

Any help will be appreciated.

答案1

得分: 1

将以下行添加到Beam构造函数中:

loadModel = new LoadModel(null, null);
英文:

Add to Beam constructor this line:

loadModel = new LoadModel(null, null);

答案2

得分: 0

Uhuh!在 Beam 类中,您的 loadModel 引用似乎为空。您需要在 Beam 类的构造函数中进行初始化。

英文:

Uhuh! Your loadModel reference seems to be null in the Beam class. You need to initialize that in either the Beam&#39;s constructor.

答案3

得分: 0

你声明一个成员变量如下:

private LoadModel loadModel;

然后稍后你尝试调用它的方法:

loadModel.addPL(PL)

由于NullPointerException,此操作将失败,因为你无法调用不存在的对象的方法。因此,要解决你的实际问题,你需要通过调用LoadModel的构造函数来初始化loadModel

你可以立即知道数组大小是否是实际的问题,因为尝试在数组范围之外引用数组元素将抛出ArrayIndexOutOfBoundsException,而不是NullPointerException

在现在修复了空指针异常问题之后,你可能会遇到其他问题,但让我们在它们实际开始引起问题时再考虑这些问题。你可以通过创建一个所需大小的相同类型新数组,将其元素初始化(通过复制源数组的元素),然后将新数组分配为数组变量的新值来“调整”数组大小。

英文:

You declare a member like

private LoadModel loadModel;

and then later you try to call its method like

loadModel.addPL(PL)

which fails due to NullPointerException, because you cannot call a method of that which does not exist. So, to solve your actual problem, you need to initialize loadModel by calling the constructor of LoadModel.

You can instantly know whether the array size is the actual problem, because trying to refer to an element of an array outside of bounds will throw an ArrayIndexOutOfBoundsException, not a NullPointerException.

After you fix your NPE problem now, you might encounter other problems, but let's worry about them when they actually start to cause problems. You can "resize" an array by creating a new array of the desired size of the same type, initializing its elements as you will (by copying the elements of the source) and then assign the new array as the new value of your array variable.

huangapple
  • 本文由 发表于 2020年8月20日 21:29:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63506185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定