none
NullReferenceException 未處理?? RRS feed

  • 問題

  • 以下是程式碼:
    在執行到第13行時出現
    NullReferenceException 未處理

    是少打了甚麼嗎?
    哪裡有錯??

    1    Dim mdx As Integer, mdy As Integer
    2    Dim g As Graphics                           '繪圖物件
    3    Dim p As Pen                                '筆
    4    Dim Pcolor As Color = Color.Black           '筆的顏色
    5    Dim Pwidth As Integer = 1                   '筆的粗細
    6    Dim bmp As Bitmap


    7    Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    8        mdx = e.X
    9        mdy = e.Y
    10    End Sub

    11    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
            If e.Button = Windows.Forms.MouseButtons.Left Then
    12            p = New Pen(Pcolor, Pwidth)
    13           g.DrawLine(p, mdx, mdy, e.X, e.Y)     <<<<<在這邊出現(NullReferenceException未處理)
    14            mdx = e.X
    15           mdy = e.Y
    16            PictureBox1.Image = bmp
    17        End If

    18    End Sub

    2009年7月20日 上午 07:04

解答

  • Public Class Form1
        Dim mdx As Integer, mdy As Integer
        Dim g As Graphics                           '繪圖物件
        Dim p As Pen                                '筆
        Dim Pcolor As Color = Color.Black           '筆的顏色
        Dim Pwidth As Integer = 1                   '筆的粗細
        Dim bmp As Bitmap
    
        Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
            mdx = e.X
            mdy = e.Y
        End Sub
    
        Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
            If e.Button = Windows.Forms.MouseButtons.Left Then
                bmp = New Bitmap(PictureBox1.Image)
                g = Graphics.FromImage(bmp)
                p = New Pen(Pcolor, Pwidth)
                g.DrawLine(p, mdx, mdy, e.X, e.Y)
                mdx = e.X
                mdy = e.Y
                PictureBox1.Image = bmp
            End If
        End Sub
    End Class
    

    .NET菜鳥自救會 http://www.dotblogs.com.tw/chou/
    • 已標示為解答 Lolota Lee 2009年7月27日 上午 05:54
    2009年7月20日 上午 07:27
    版主

所有回覆

  • HI,

    g未設定

    前面加上

    g=Graphics.FromImage(bmp)

    不過bmp你好像也沒New就是了

    http://www.dotblogs.com.tw/larrynung/
    2009年7月20日 上午 07:12
  • Public Class Form1
        Dim mdx As Integer, mdy As Integer
        Dim g As Graphics                           '繪圖物件
        Dim p As Pen                                '筆
        Dim Pcolor As Color = Color.Black           '筆的顏色
        Dim Pwidth As Integer = 1                   '筆的粗細
        Dim bmp As Bitmap
    
        Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
            mdx = e.X
            mdy = e.Y
        End Sub
    
        Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
            If e.Button = Windows.Forms.MouseButtons.Left Then
                bmp = New Bitmap(PictureBox1.Image)
                g = Graphics.FromImage(bmp)
                p = New Pen(Pcolor, Pwidth)
                g.DrawLine(p, mdx, mdy, e.X, e.Y)
                mdx = e.X
                mdy = e.Y
                PictureBox1.Image = bmp
            End If
        End Sub
    End Class
    

    .NET菜鳥自救會 http://www.dotblogs.com.tw/chou/
    • 已標示為解答 Lolota Lee 2009年7月27日 上午 05:54
    2009年7月20日 上午 07:27
    版主