我来自 C 背景,在 Java 中遇到了问题。目前,我需要在对象数组中初始化变量数组。
我知道在 C 中它类似于malloc-ing
数组中int
的数组,structs
如:
typedef struct {
char name;
int* times;
} Route_t
int main() {
Route_t *route = malloc(sizeof(Route_t) * 10);
for (int i = 0; i < 10; i++) {
route[i].times = malloc(sizeof(int) * number_of_times);
}
...
到目前为止,在 Java 中我有
public class scheduleGenerator {
class Route {
char routeName;
int[] departureTimes;
}
public static void main(String[] args) throws IOException {
/* code to find number of route = numRoutes goes here */
Route[] route = new Route[numRoutes];
/* code to find number of times = count goes here */
for (int i = 0; i < numRoutes; i++) {
route[i].departureTimes = new int[count];
...
但它吐出一个NullPointerException
. 我做错了什么,有没有更好的方法来做到这一点?