Resolved "System.NullReferenceException" error when combobox1.items.add

HoneyMan

New member
Joined
Sep 25, 2022
Messages
3
Programming Experience
10+
Hellow everyone.
I'm on moving to vb.net from vba.

But I am facing a problem, help me please,...

error:
        '이미지 파일 목록 콤보상자 재구성
        If IsNothing(ComboBox1) Then
            PictureBox10.Image = Nothing
        Else
            ComboBox1.Items.Clear()
        End If
        With ComboBox1
            If prmVariationCode = "-999" Then
                '모든 사진
                For J = 0 To PageSet(0).Pages(CurPage).Products(0).PhotoCount - 1
ERR ----->   .Items.Add("1") 
                    .Items(J) = J + 1 & "/" & PageSet(0).Pages(CurPage).Products(0).PhotoCount & " " & PageSet(0).Pages(CurPage).Products(0).Photos(J).FileName
                    If J + 1 = PageSet(0).Pages(CurPage).Products(0).curPhotoSeq Then
                        .SelectedIndex = J
                    End If
                Next J
            Else
                '해당 variation에 속하는 사진들만,..
                For J = 1 To PageSet(0).Pages(CurPage).Products(0).PhotoCount
                    .Items.Add("[" & J & "/" & PageSet(0).Pages(CurPage).Products(0).PhotoCount & "] " & PageSet(0).Pages(CurPage).Products(0).Photos(J).FileName)
                    If prmVariationCode = PageSet(0).Pages(CurPage).Products(0).Photos(J).VariationCode Then
                        .SelectedIndex = J
                        PageSet(0).Pages(CurPage).Products(0).curPhotoSeq = J + 1
                    End If
                Next J
            End If
        End With

1664162057266.png

this code make an error "System.NullReferenceException".
I searched for 3 hours but ,....
Please,.....
 
Last edited:
A NullReferenceException is one of the easiest exceptions to diagnose because the process is always the same. When the exception is thrown, simply use the debugger to examine every reference on the offending like to determine which one is Nothing. You then work backwards to where you expected that reference to be set and determine why it wasn't. You should never be posting a question about such an exception until you can, at the very least, tell us which reference on which line is Nothing and where you expected that reference to be set.

That said, I can see an obvious flaw in your code right away. You have this:
VB.NET:
If IsNothing(ComboBox1) Then
    PictureBox10.Image = Nothing
Else
    ComboBox1.Items.Clear()
End If
With ComboBox1
You start by testing whether ComboBox1 is Nothing, which suggests that you already know that it can be. After that, you just go ahead and use ComboBox1. Why, then, should you be surprised if you get a NullReferenceException when you use a reference that you already know might be Nothing?
 
A NullReferenceException is one of the easiest exceptions to diagnose because the process is always the same. When the exception is thrown, simply use the debugger to examine every reference on the offending like to determine which one is Nothing. You then work backwards to where you expected that reference to be set and determine why it wasn't. You should never be posting a question about such an exception until you can, at the very least, tell us which reference on which line is Nothing and where you expected that reference to be set.

That said, I can see an obvious flaw in your code right away. You have this:
VB.NET:
If IsNothing(ComboBox1) Then
    PictureBox10.Image = Nothing
Else
    ComboBox1.Items.Clear()
End If
With ComboBox1
You start by testing whether ComboBox1 is Nothing, which suggests that you already know that it can be. After that, you just go ahead and use ComboBox1. Why, then, should you be surprised if you get a NullReferenceException when you use a reference that you already know might be Nothing?

1:
'This is previous code, but error occured first time
ComboBox1.Items.Clear()

'this is modified code, after i see error first time
If IsNothing(ComboBox1) Then
    PictureBox10.Image = Nothing
Else
    ComboBox1.Items.Clear()
End If

'so i thought now it's ok, but second error occured'
       With ComboBox1
            If prmVariationCode = "-999" Then
                '모든 사진
                For J = 0 To PageSet(0).Pages(CurPage).Products(0).PhotoCount - 1
                    .Items.Add("1")  << error occured
 
It's resolved.
I found "ComboBox1 = Nothing".
This code positioned top of the code I mentioned on this post.

Thanks everyone.
 
We're happy to help but please be sure to do appropriate debugging before posting a question. That would have revealed the issue without your having to post a question at all. Perhaps you weren't aware of what was actually involved in debugging but hopefully now you are.
 
Back
Top