由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 类里的字符串向量赋值,总报错:
相关主题
类里的变量容易被以下哪个操作修改?怎么在写类的时候表示某个抽象的对象呢?
关于对象向量删除/添加local变量被赋值了几次?
大家写java class的时候是完全封装的么?help: 两个Java的问题
如何读懂Java程序use >>>= with short
微软面试 zt请教GUI程序的save/load功能的实现
[合集] 怎样拷贝一个二维数组?那个数组找duplicate的面试题
能否让函数返回一个用于赋值的引用请教一题
static object assignment/init ?为什么结果是3?
相关话题的讨论汇总
话题: java话题: pointers话题: object话题: mp话题: reference
进入Java版参与讨论
1 (共1页)
l***e
发帖数: 480
1
类里的字符串向量赋值,总报错:
java.lang.NullPointerException
定义了一个类:
CLASS MP{
Vector AAA;
.....
};
.....
String STR=“XXXXX”;
MP mp1= new MP();
mp1.AAA.add(STR); 〈--- EXCEPTION
什么问题?
g*****g
发帖数: 34805
2
In MP's constructor, you need to init AAA.
public MP() {
AAA = new Vector();
}

【在 l***e 的大作中提到】
: 类里的字符串向量赋值,总报错:
: java.lang.NullPointerException
: 定义了一个类:
: CLASS MP{
: Vector AAA;
: .....
: };
: .....
: String STR=“XXXXX”;
: MP mp1= new MP();

l***e
发帖数: 480
3
3x a lot.
I thought I found a huge bug.
A**o
发帖数: 1550
4
at your level, whenever there is a problem, it most likely your fault. :)

【在 l***e 的大作中提到】
: 3x a lot.
: I thought I found a huge bug.

w*****d
发帖数: 2415
5
No. 1 errors made by new Java programmers: Null pointer exception.

【在 g*****g 的大作中提到】
: In MP's constructor, you need to init AAA.
: public MP() {
: AAA = new Vector();
: }

a****l
发帖数: 8211
6
Yes, that's an interesting thing: Java always claims programmer do not need
"pointer", but actually in Java there are pointers everywhere. If you treat
Java like C++, you would avoid lots of similar problems.

【在 w*****d 的大作中提到】
: No. 1 errors made by new Java programmers: Null pointer exception.
m******t
发帖数: 2416
7

need
treat
Well technically they are called "references", so NullPointerException
really
is a misnomer and should've been named NullReferenceException instead.

【在 a****l 的大作中提到】
: Yes, that's an interesting thing: Java always claims programmer do not need
: "pointer", but actually in Java there are pointers everywhere. If you treat
: Java like C++, you would avoid lots of similar problems.

g*****g
发帖数: 34805
8
It's very different as:
1. You can easily get the error stack to locate the error, no dumb core dump.
2. It's much safer when you have an Null pointer error in java, in C++ you
can potentially screw the data in the memory with dangling pointer. So Java
program is more robust on the server side.

need
treat

【在 a****l 的大作中提到】
: Yes, that's an interesting thing: Java always claims programmer do not need
: "pointer", but actually in Java there are pointers everywhere. If you treat
: Java like C++, you would avoid lots of similar problems.

a****l
发帖数: 8211
9
depending on perspective, I would say the huge difference is in only in the
implementation , not in the concept. My understanding is just that pointers
are much safer to use in Java than in C++, but they are still pointers.
BTW, I think calling "pointers" as "references" does not make them anything
less than a pointer. I would stick to the C++ definition that "reference"
is only the pointer pointing to a real object. I just feel that "
nullreference" thing is a badly guided effort of Java/Sun d

【在 g*****g 的大作中提到】
: It's very different as:
: 1. You can easily get the error stack to locate the error, no dumb core dump.
: 2. It's much safer when you have an Null pointer error in java, in C++ you
: can potentially screw the data in the memory with dangling pointer. So Java
: program is more robust on the server side.
:
: need
: treat

g*****g
发帖数: 34805
10
nah, it doesn't matter how you want to call it. You have a function,
the function is supposed to return an object, but sometimes there's
an error in the runtime and you cannot, what can you do? You have to
return a null or something similar.
Now if the caller doesn't check, it's going to get a nullpointer exception.
You can call it something different but wouldn't make a difference.
I don't think this can be prevented by this generation of language.

the
pointers
anything
claim

【在 a****l 的大作中提到】
: depending on perspective, I would say the huge difference is in only in the
: implementation , not in the concept. My understanding is just that pointers
: are much safer to use in Java than in C++, but they are still pointers.
: BTW, I think calling "pointers" as "references" does not make them anything
: less than a pointer. I would stick to the C++ definition that "reference"
: is only the pointer pointing to a real object. I just feel that "
: nullreference" thing is a badly guided effort of Java/Sun d

m******t
发帖数: 2416
11

the
pointers
It's quite the other way around IMO. The differences are more between the
concepts than the implementations. References (or "pointers in Java") are
largely still implemented as actual memory pointers, but conceptually a
reference does not point to a particular memory location, and that leads
to a couple of important distinctions:
1. you cannot offset a reference like you would a memory pointer, and
2. a reference references an object, so when the object moves, the reference
follows

【在 a****l 的大作中提到】
: depending on perspective, I would say the huge difference is in only in the
: implementation , not in the concept. My understanding is just that pointers
: are much safer to use in Java than in C++, but they are still pointers.
: BTW, I think calling "pointers" as "references" does not make them anything
: less than a pointer. I would stick to the C++ definition that "reference"
: is only the pointer pointing to a real object. I just feel that "
: nullreference" thing is a badly guided effort of Java/Sun d

a****l
发帖数: 8211
12
2 is an interesting point, but I have never heard of that. Does that mean if
Object obj=new Object(),
then the Object is moved to some other memory address because of system
memory operations, the obj reference would be changed also? Is there any
example or reference material for this?

reference

【在 m******t 的大作中提到】
:
: the
: pointers
: It's quite the other way around IMO. The differences are more between the
: concepts than the implementations. References (or "pointers in Java") are
: largely still implemented as actual memory pointers, but conceptually a
: reference does not point to a particular memory location, and that leads
: to a couple of important distinctions:
: 1. you cannot offset a reference like you would a memory pointer, and
: 2. a reference references an object, so when the object moves, the reference

m******t
发帖数: 2416
13

if
Yes, this happens during every gc cycle. The garbage collector would pack
all the objects to be kept around towards one end of the heap, so that
subsequent memory allocations are cheap and heap fragmentation is minimal.

【在 a****l 的大作中提到】
: 2 is an interesting point, but I have never heard of that. Does that mean if
: Object obj=new Object(),
: then the Object is moved to some other memory address because of system
: memory operations, the obj reference would be changed also? Is there any
: example or reference material for this?
:
: reference

1 (共1页)
进入Java版参与讨论
相关主题
为什么结果是3?微软面试 zt
问一个java基础的初始化的问题,一直搞不明白 (转载)[合集] 怎样拷贝一个二维数组?
C#的Dictionary赋值操作必须先ContainsKey检查吗?能否让函数返回一个用于赋值的引用
C++编程问题:union inside structstatic object assignment/init ?
类里的变量容易被以下哪个操作修改?怎么在写类的时候表示某个抽象的对象呢?
关于对象向量删除/添加local变量被赋值了几次?
大家写java class的时候是完全封装的么?help: 两个Java的问题
如何读懂Java程序use >>>= with short
相关话题的讨论汇总
话题: java话题: pointers话题: object话题: mp话题: reference