Results 1 to 13 of 13

Thread: [RESOLVED] List of New Custom Object

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Resolved [RESOLVED] List of New Custom Object

    Ok be easy on me, this is simple for most, but I'm having trouble.

    I created a simple (for now) custom object:

    VB Code:
    1. Public Class Form1
    2. Dim AllPartFiles As List(Of Part)
    3. .
    4. .
    5. .
    6.  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7. '**NEED HELP HERE**
    8. End Sub
    9. .
    10. .
    11. .
    12. End Class '[Form1]
    13.  
    14. Public Class Part
    15.     Public Shared FullFilePath As String
    16.     Public Shared Part Number as Integer
    17.     Public Shared Description as String
    18. End Class '[Part]

    As I search a folder for files, it returns FilePath as a string for each file in the folder, then assigns a new Part.FullFilePath to the current returned FilePath, then add the Part to AllPartFiles list. So I can retrieve each Part from AllPartFiles later. But everything I try doesnt work.

    I know below is wrong, but might show what Im trying to do:
    VB Code:
    1. Dim Files() As String
    2.         Files = System.IO.Directory.GetFiles(RootFolder, "*.ipt")
    3.         Dim k As String
    4.         For Each k In Files
    5.             Part.FullFilePath = k
    6.             AllPartFiles.Add(New Part)
    7.         Next
    8.         Dim j As Part
    9.         For Each j In AllPartFiles
    10.             MsgBox(j.Count)
    11.         Next

    So how would you do this? Create a New Part, change its values, and then add it to AllPartFiles list for later, then repeat?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: List of New Custom Object

    first... your properties should NOT be shared... making them shared means that there will only ever be one copy. Just make them public, and that should be enough.

    You're close with your loop... but you ran into problems because of your shared properties. After you've removed the shared qualifier, try this:
    Code:
    For each k in files
      Dim newPart As New Part ' This creates a new instance of your part class
      newPart.FullFilePath = k 'This sets the property
      AllPartFiles.Add(newPart) 'note that is newPart, the name of the variable... not New Part :)
    Next
    Also, if what you are trying to get is the count of the items in your list, it's just AllPartFiles.Count... otherwise you're getting the Count property of your j varaible... with is a Part... and doesn't have a count property.

    And I just noticed you have a property called "Part Number"... that shouldn't work... you can't put spaces in property names like that... jsut call it PartNumber.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: List of New Custom Object

    You need to create the part, THEN add it:
    Code:
    Dim k As String
    dim p As Part
            For Each k In Files
                p= New Part
                p.FullFilePath = k
                AllPartFiles.Add(p)
            Next
    My usual boring signature: Nothing

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: List of New Custom Object

    Doggone it. I got called away by a magazine salesman. I knew somebody would post ahead of me, so I just hastened it up.

    On the other hand, the salesman was the most entertaining of the sort that I have ever encountered. Didn't buy anything, but that kid has a future.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: List of New Custom Object

    @tg -
    Thanks for the help. I'm still a beginner programmer so this is still a learning process for me. Not sure why I used "shared", maybe b/c some sample code on a forum somewhere used it. As for "Part Number" I just quickly typed that in and put the space in there by accident. Thanks for the help!

    MotoMan

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: List of New Custom Object

    Ok, still running into a problem. I have this:
    VB Code:
    1. Dim AllPartFiles As List(Of Part)
    2. .
    3. .
    4. .
    5.        Dim Files() As String
    6.         Files = System.IO.Directory.GetFiles(RootFolder, "*.ipt")
    7.         Dim k As String
    8.         Dim newPart As Part
    9.         For Each k In Files
    10.             newPart = New Part
    11.             newPart.FullFilePath = k
    12.             AllPartFiles.Add(newPart)
    13.         Next

    And I'm getting this error: "Object reference not set to an instance of an object."

    Nesting the "Dim newPart As Part" or having it before the "For Each" yields same result

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: List of New Custom Object

    this is where you need to develop some debugging skills... .first thing... find out which of your objects doesn't exist yet... I know which one it is... I spotted it the first time... but this is where you need to start learning a few tricks. Hint: It isn't related to Part... but something else on the line ... remember objects must be created before you can use them... diming the variable isn't enough...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: List of New Custom Object

    Another hint would be: Lists are objects.

    This is actually a VERY common error to get, by the way. I would say that it is the most commonly posted error message by a very wide margin. I would expect that most of us get them from time to time (I have even gotten that exception by running a particular query in SQL Server), so knowing how to investigate it is a critical skill. The steps I prefer to take are these:

    1) Put a breakpoint on the line that throws the exception.

    As an alternative, you could just wait until the exception is raised, but I like getting a look at the line prior to the exception. For some complex lines that involve function calls, it can give you slightly better information...maybe...I haven't fully thought it through. I guess I just prefer the breakpoint.

    2) Highlight each object in turn. You may have to press Shift+F9 to see what is in the variable. Sometimes it shows up as a tooltip, sometimes it doesn't, at least that's how it works for me.

    In your case, you are getting the exception on this line:

    AllPartFiles.Add(newPart)

    The two pieces to look at are:

    AllPartFiles and newPart

    The Add is a method, not an object, so it isn't worth a look. One of those two objects is Nothing. That will tell you which object was not created prior to use.

    These same steps can be used to investigate any instance of this particular exception.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: List of New Custom Object

    @tg, I applaud you in forcing people to LEARN rather than giving the answer (with these forums, it's too easy to grab the answer and run without actually learning anything)

    Now let me break it down, and see where my flaw might be.....
    VB Code:
    1. Dim Files() As String 'Created, but not set to anything yet
    2.         Files = System.IO.Directory.GetFiles(RootFolder, "*.ipt") 'now "Files" is set to what GetFiles returned
    3.         Dim k As String 'Created but not set
    4.         Dim newPart As Part 'Created as Part, but not set
    5.         For Each k In Files 'Sets k to next entry in Files
    6.             newPart = New Part 'Sets newPart to look like Part, which is still not set
    7.             newPart.FullFilePath = k 'Sets string to k value
    8.             AllPartFiles.Add(newPart) 'Adds newPart to AllPartFiles list
    9.         Next

    And..... I'm still lost...Crap.... The only thing I could think of is newPart object isnt defined yet? Even tho it's housed string is? Yet it does bark at me saying "newPart is not defined".... Didnt I define it when I set the newPart.FillFilePath?

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: List of New Custom Object

    You defined it here: Dim newPart As Part
    You instanciated it here: newPart = New Part

    Are you *SURE* it's newPart that is saying is not defined? And not maybe AllPartFiles? double-click AllPartFiles to highlight it, then right-click and select "Quick Watch" ... see what it says.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: List of New Custom Object

    Well AllPartFiles is define in the beginning to be a list of "Part"s, and Im adding newPart which is a Part... Using analogies, I'm defining AllPartFiles as a tray that can only hold Parts, nothing else will fit. So in the beginning I have an empty tray. Then later I'm adding a Part to the tray. So I dont see whats wrong. What am I missing? Is my understanding of a list wrong? I'm guessing I defined the tray, yet havent created a tray to hold the parts?

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: List of New Custom Object

    Ok, with what you said, sparked something... So I did this....

    Dim AllPartFiles As New List(Of Part)

    And it works! But whats the difference? Why does defining it as a "New" list any different than just "List"?

  13. #13
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: List of New Custom Object

    Don't get caught up in the word 'List(Of Part)'. A List(Of Part) is just a class, just like Part is a class. The (Of ...) part means that it is a generic class, and the '...' can be replaced by any other type. You could have a List(Of String) or List(Of Integer) or List(Of List(Of Part)).

    So, now that you know that a List(Of Part) is just another class, you can apply the same principles that you already know:

    If you want to use a Part, you do two things:
    1. You declare a variable that holds a Part
    2. You assign a new instance of the Part class to that variable.
    Code:
    Dim p As Part  '1.
    p = New Part()  '2.
    You can shorten this to
    Code:
    Dim p As Part = New Part()
    or even
    Code:
    Dim p As New Part()
    which means exactly the same, but is just a little 'syntactic sugar'.

    After the first line, all you have is some empty 'space' for a Part to exist in. But there isn't any Part in that space yet. Only after line 2 have you created a new Part and assigned it to the variable.

    It works exactly the same with a list:
    1. You declare a variable that can hold a List(Of Part)
    2. You assign a new instance of the List(Of Part) class to that variable
    Code:
    Dim parts As List(Of Part)
    parts = New List(Of Part)
    and again, you can shorten this to
    Code:
    Dim parts As List(Of Part) = New List(Of Part)
    or even
    Code:
    Dim parts As New List(Of Part)
    and that's what you got.


    So, you forgot to do step 2: all you had was an 'empty space' (called 'null' as the error mentions), you never assigned a list to that space. Then, you try to call the Add method of 'empty space', and this is what causes the null reference exception (you can't do that on 'nothing' obviously).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width